| | |
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 |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






