| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| deltatech 
 
 
 Joined: 22 Apr 2006
 Posts: 87
 
 
 
			    
 
 | 
			
				| Bitwise Masking wont work Please help ! |  
				|  Posted: Wed Aug 16, 2006 1:07 pm |   |  
				| 
 |  
				| Hi All i want to extract from this number 
 19495316080623
 
 194953
 
 and also
 
 1606
 
 I have tried to use bitwise and but it doesnt work .
 
 Can any please tell me what i am doing wrong ?
 
 Thanks in advance
 
 
  	  | Code: |  	  | #include "F:\Program Files\PICC\Project\MSF\Mask.h" 
 
 void main()
 {
 int  received number=19495316080623
 
 
 int time
 int date
 
 time=19495316080623&FFFFFF;
 
 date=19495316080623&000000ff00ff00;
 
 printf(%d,time);
 
 printf(%d,date);
 
 
 }
 | 
 |  | 
	
		|  | 
	
		| rberek 
 
 
 Joined: 10 Jan 2005
 Posts: 207
 Location: Ottawa, Canada
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Aug 16, 2006 1:40 pm |   |  
				| 
 |  
				| Normally you perform a bitwise AND on two numbers with a similar radix (i.e. both hex).  I'm guessing that doing this with a decimal integer and a hex number means the compiler will likely convert the decimal integer to a hex value, do the AND, then the printf will turn the hex value in to an integer, which will look nothing like your expected result. 
 In addition, looking at your example, you seem to expect that the bits that are masked out will magically disappear.  They don't.  If you are ANDing two hex values, the masked bits become zero and are still there.  You would need to perform other operations (i.e. right shifts) to extract and use the unmasked bits.
 
 I'm guessing you would likely want to turn your value into an ASCII string and do some C string manipulation instead, but that's not my forte, so I could be wrong.
 |  | 
	
		|  | 
	
		| rnielsen 
 
 
 Joined: 23 Sep 2003
 Posts: 852
 Location: Utah
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Aug 16, 2006 2:22 pm |   |  
				| 
 |  
				| Try using sprintf() to convert the number to a char (or rather a string) and then simply index through the characters, stuffing them where you want, and then convert the string back into an integer. 
 Ronald
 |  | 
	
		|  | 
	
		| rwyoung 
 
 
 Joined: 12 Nov 2003
 Posts: 563
 Location: Lawrence, KS  USA
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Wed Aug 16, 2006 2:25 pm |   |  
				| 
 |  
				| Disable HTML when posting code, and continue to use the CODE button. 
 That said, the code you posted won't compile.  The declaration is invalid, you can't have a space in a variable name and the number you are trying to initialize with is greater than 255, the largest 8-bit integer you can have.  The default word size is 8-bits for the "int" data type in CCS.
 
 From there it just gets worse.  So I'm guessing the problem is your code posting got munged.
 _________________
 Rob Young
 The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
 |  | 
	
		|  | 
	
		|  |