Page 1 of 1
Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 6, 2001 @ 1:25am
by RwGast
Does anyone understand this stuff i can count strait up in hex and i know the binary number system from my cisco class pretty well. But i dont understand how to use hex as flags and i really dont understand the pupose or bitwise operators i know what they do but why use them?
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 6, 2001 @ 4:01am
by PDAFantast
Numerical representation of FALSE is zero. Numerical representation of TRUE is either one or minus one, or any other non-zero standard.<br><br>When you're doing:<br><br>if(flag){}<br><br>you're really checking if the flag variable is non-zero, upon which the if statement is executed.<br>This is equivalent to the following statement:<br><br>if( flag != 0 )<br><br>In C, true and false are defined as:<br><br>#define TRUE 1<br>#define FALSE 0<br><br>whereas there is a defined type Boolean<br><br>typedef int Boolean;<br><br>Bitwise operators are used for accessing certain bits in a numerical entity. It's used in different areas, some of them being areas where you need to access only a certain bit (hardware registers), or bit per bit accessing a file format structure, or transformation of input data, or for example conversion from big endian to little endian format. <br><br>Ofcourse, there are millions of instances where you use bitwise operations, so I suggest you get a hang of it pretty soon

<br><br>Say, you have a longword (32bits) and you only want the lowest 8 bits (byte). Look at this C snippet showing the & (AND) operator:<br><br>long a = 0xBAADF00D;<br>unsigned char b;<br><br>b = a & 0xFF; // 0x000000FF<br><br><br>Variable b will now contain 0x0D.<br><br>>> is the bitwise shift right operator. It will shift the bitmask as many positions as you want to the right. << does the same to the left.<br><br>b = (a & 0xFF0000) >> 16;<br><br>will give b the value 0xAD. First the paranthesis will result in 0x00AD0000 and then the shift to right will result in 0x000000AD.<br><br><< and >> also have the effect of multiplying or diving by the power of 2 multiples.<br><br>a = 6;<br>a = a << 1; // times 2<br><br>a equals now 12;<br><br>a= 5;<br>a = a << 2; // times 4<br><br>a equals now 20.<br><br>a = 16;<br>a = a >> 3; // divided by 8<br><br>a equals 2.<br><br>the | (OR) operator is used for setting bits (AND was used for clearing bits):<br><br>unsigned int a = 0xFF00;<br>a = a | 0xAC;<br><br>a equals now 0xFFAC.<br><br>Bits that are already set, remain set.<br><br>The ~ (NOT) operator isused to reverse the bits from zero to 1 and viceversa.<br><br>a = 0x00FF00FF;<br>a = ~a<br><br>a equals 0xFF00FF00.<br><br>Let me see if you understood these things...<br>What is the value of a after these statements:<br><br>long a = 0x55AA55AA;<br>long b = 0xBAADF00D;<br><br>a = (a & b) | (~a >> 16);<br><br>Hope I haven't been a pain in the ass

<br>
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 6, 2001 @ 11:10am
by Moose or Chuck
and bitwise shifting is faster than multiplication or division<br><br>(I had to add something!) ;D
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 6, 2001 @ 12:20pm
by RICoder
BITFLAGS:<br> Say you want to return information or set information, but only use one variable. Well, it's like this. <br>===============================================<br> #define DEF_COLOR 0x1 //BIN = 00000001<br> #define DEF_BW 0x0 //BIN = 00000000<br> #define DEF_FULLSCREEN 0x2 //BIN = 00000010<br> #define DEF_SPRITE 0x0 //BIN = 00000000<br> #define DEF_OPT1 0x4 //BIN = 00000100<br><br> Then you can say <br> INT x = DEF_COLOR | DEF_FULLSCREEN<br> And you get<br> x = 00000011<br> So if you check it you see that<br> x & DEF_FULLSCREEN > 0 //so the flag is on<br> AND<br> x & DEF_COLOR > 0 //so the flag is on<br>===============================================<br>BITWISE OPERATORS (SHIFTING)<br> First off & and | are used to do BITWISE AND and OR, see above and you can see what this is about.<br> Next is the shift, either << or >>. Why? Well, say you are converting WORD into two differnt values (Windows does this with lParams and wParams all the time). Well you can do this.<br> BYTE x = lParam<br> BYTE y = lParam << 4<br>==================================================
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 6, 2001 @ 4:22pm
by RwGast
Thanks guys between this and some online articles i think im about there, boy ill tell you ive known c for like 2 years and never understood this stuff, and i even know how to write windows apps in c with strait api, thanks to vb for teaching me api and you guys always knock vb. Now that im making the transition from C to C++ i guess i had better learn it.
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 7, 2001 @ 9:27am
by Dan East
No one showed how to clear a bit(s).<br><br>[fixed]<br>#define FLAG_1 0x0001<br>#define FLAG_2 0x0010<br><br>//Set both flag 1 and flag 2<br>int flags=FLAG_1|FLAG_2;<br><br>//Clear flag 1<br>flags&=~FLAG_1<br>[/fixed]<br><br>Dan East
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 7, 2001 @ 9:36am
by Dan East
Another thing to add; if you are using bits for flags, make sure that each flag is only one bit. You mentioned that you know hex, so you probably already understand:<br>[fixed]<br>Hex Binary<br>0 0000<br>1 0001<br>2 0010<br>3 0011<br>4 0100<br>5 0101<br>6 0110<br>7 0111<br>8 1000<br>9 1001<br>A 1010<br>B 1011<br>C 1100<br>D 1101<br>E 1110<br>F 1111<br>[/fixed]<br><br>So you can see that hex values 1 2 4 and 8 are particularly valuable, because each of those hex numbers can be represented by a single bit. Those are the hex values you usually use for flags (unless you know what you are doing and use a value that sets 2 or more flags at a time).<br><br>Finally, do not start using binary flags in place of boolean type / operators, just because you can cram 32 true / false flags into a single int. Generally flags are used to pass many boolean values to a function, instead of passing a zillion parameters. It takes a lot more processing to extract / set binary flags as opposed to checking / setting the value of an entire int. So in other words, don't use flags in areas where your code is iterated often (games).<br><br>Dan East
Re: Hex, Flags, Bitwise Operators?? Im lost!!

Posted:
Jul 12, 2001 @ 4:51pm
by RICoder
Why does it feel like I am back in school? I haven't read this much about Hex and Binary in about 10 years....<br>And people think that 4GLs are taking over! HA!<br><br>I love this....