This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Hex, Flags, Bitwise Operators?? Im lost!!


Hex, Flags, Bitwise Operators?? Im lost!!

Postby RwGast » Jul 6, 2001 @ 1:25am

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?
http://www.angelfire.com/ego/esoteric if you like to play quake3 keep your eye on this site
User avatar
RwGast
pm Member
 
Posts: 1123
Joined: Jun 28, 2001 @ 7:36pm
Location: California, USA


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby PDAFantast » Jul 6, 2001 @ 4:01am

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>
PDAFantast.
PDAFantast
pm Member
 
Posts: 244
Joined: Jun 27, 2001 @ 4:00pm
Location: Malmö, Sweden


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby Moose or Chuck » Jul 6, 2001 @ 11:10am

and bitwise shifting is faster than multiplication or division<br><br>(I had to add something!) ;D
Moose or Chuck
 


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby RICoder » Jul 6, 2001 @ 12:20pm

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>==================================================
RICoder
 


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby RwGast » Jul 6, 2001 @ 4:22pm

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.
http://www.angelfire.com/ego/esoteric if you like to play quake3 keep your eye on this site
User avatar
RwGast
pm Member
 
Posts: 1123
Joined: Jun 28, 2001 @ 7:36pm
Location: California, USA


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby Dan East » Jul 7, 2001 @ 9:27am

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
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby Dan East » Jul 7, 2001 @ 9:36am

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
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: Hex, Flags, Bitwise Operators?? Im lost!!

Postby RICoder » Jul 12, 2001 @ 4:51pm

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....
<iframe src="http://gamercard.xbox.com/RICoder.card" scrolling="no" frameBorder="0" height="140" width="204">RICoder</iframe>
User avatar
RICoder
FOX News Correspondent
 
Posts: 3948
Joined: Jul 10, 2001 @ 1:48pm
Location: the matrix has me


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron