| | |
Problem with Bitwise Masking
![]() |
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
Ok so I make my masks
and then I test my masks
when I test it the values come out as
which is equal to
which is messing up my checksum program when I try to apply the masks. :-\
Any advice?
C Syntax (Toggle Plain Text)
short m1, m2, m3, m4; m1 = 0xF000; m2 = 0x0F00; m2 = 0x00F0; m3 = 0x000F;
and then I test my masks
C Syntax (Toggle Plain Text)
printf("%hd %hd %hd %hd\n", m1, m2, m3, m4);
when I test it the values come out as
C Syntax (Toggle Plain Text)
-4096 240 15 0
which is equal to
C Syntax (Toggle Plain Text)
0xF000 0x00F0 0x000F 0x0000
which is messing up my checksum program when I try to apply the masks. :-\
Any advice?
0
#2 17 Days Ago
•
•
•
•
Ok so I make my masks
C Syntax (Toggle Plain Text)
short m1, m2, m3, m4; m1 = 0xF000; m2 = 0x0F00; m2 = 0x00F0; m3 = 0x000F;
and then I test my masks
C Syntax (Toggle Plain Text)
printf("%hd %hd %hd %hd\n", m1, m2, m3, m4);
when I test it the values come out as
C Syntax (Toggle Plain Text)
-4096 240 15 0
which is equal to
C Syntax (Toggle Plain Text)
0xF000 0x00F0 0x000F 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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main(int argc, char**argv) { short m1 = 0xf000, m2 = 0x0f00, m3 = 0x00f0, m4 = 0x000f; printf("%hd %hd %hd %hd\n", m1, m2, m3, m4); exit(EXIT_SUCCESS); }
What are you using for a compiler?
Last edited by gerard4143; 17 Days Ago at 2:14 pm.
0
#4 17 Days Ago
Used unsigned integral types when dealing with bits.
[edit]Part of the reason for your "strange" output:
You don't give m4 a value.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { unsigned short m1 = 0xf000, m2 = 0x0f00, m3 = 0x00f0, m4 = 0x000f; printf("%hu %hu %hu %hu\n", m1, m2, m3, m4); printf("%hx %hx %hx %hx\n", m1, m2, m3, m4); return 0; } /* my output 61440 3840 240 15 f000 f00 f0 f */
•
•
•
•
Ok so I make my masks
short m1, m2, m3, m4; m1 = 0xF000; m2 = 0x0F00; m2 = 0x00F0; m3 = 0x000F;
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
![]() |
Similar Threads
- Problem IN BITWISE OPERATOR (Java)
- problem in bitwise logical operators (Java)
- where to start with perl? (Perl)
Other Threads in the C Forum
- Previous Thread: No such file or directory
- Next Thread: Zeroing bits
| Thread Tools | Search this Thread |
#include * adobe ansi array arrays asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux locate looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






