Problem with Bitwise Masking

Reply

Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Problem with Bitwise Masking

 
0
  #1
17 Days Ago
Ok so I make my masks
  1. short m1, m2, m3, m4;
  2.  
  3. m1 = 0xF000;
  4. m2 = 0x0F00;
  5. m2 = 0x00F0;
  6. m3 = 0x000F;

and then I test my masks
  1. printf("%hd %hd %hd %hd\n", m1, m2, m3, m4);

when I test it the values come out as
  1. -4096 240 15 0

which is equal to

  1. 0xF000
  2. 0x00F0
  3. 0x000F
  4. 0x0000

which is messing up my checksum program when I try to apply the masks. :-\

Any advice?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 346
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 44
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Posting Whiz
 
0
  #2
17 Days Ago
Originally Posted by Kombat View Post
Ok so I make my masks
  1. short m1, m2, m3, m4;
  2.  
  3. m1 = 0xF000;
  4. m2 = 0x0F00;
  5. m2 = 0x00F0;
  6. m3 = 0x000F;

and then I test my masks
  1. printf("%hd %hd %hd %hd\n", m1, m2, m3, m4);

when I test it the values come out as
  1. -4096 240 15 0

which is equal to

  1. 0xF000
  2. 0x00F0
  3. 0x000F
  4. 0x0000

which is messing up my checksum program when I try to apply the masks. :-\

Any advice?

I tried initializing the values like this and it worked

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char**argv)
  5. {
  6.  
  7. short m1 = 0xf000, m2 = 0x0f00, m3 = 0x00f0, m4 = 0x000f;
  8.  
  9. printf("%hd %hd %hd %hd\n", m1, m2, m3, m4);
  10.  
  11. exit(EXIT_SUCCESS);
  12. }

What are you using for a compiler?
Last edited by gerard4143; 17 Days Ago at 2:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster
 
0
  #3
17 Days Ago
gcc on a unix machine
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
17 Days Ago
Used unsigned integral types when dealing with bits.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. unsigned short m1 = 0xf000, m2 = 0x0f00, m3 = 0x00f0, m4 = 0x000f;
  7. printf("%hu %hu %hu %hu\n", m1, m2, m3, m4);
  8. printf("%hx %hx %hx %hx\n", m1, m2, m3, m4);
  9. return 0;
  10. }
  11.  
  12. /* my output
  13. 61440 3840 240 15
  14. f000 f00 f0 f
  15. */
[edit]Part of the reason for your "strange" output:
Originally Posted by Kombat View Post
Ok so I make my masks
short m1, m2, m3, m4;

m1 = 0xF000;
m2 = 0x0F00;
m2 = 0x00F0;
m3 = 0x000F;
You don't give m4 a value.
Last edited by Dave Sinkula; 17 Days Ago at 2:45 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC