This topic is now archived and is closed to further replies.

Bitwise operators

This topic is 6388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can somebody explain the bitwise operators to me? You know, the & and / and ^ thingies. I have an idea of what they do, but know idea how to use them, or why to use them. Any help/links/tuts will be appreciated......thanx. Dare wa neko o koroshiteimasuka? (Ha! Learn Nihongo!)

Share this post

Link to post
Share on other sites
This is from my perspective, programming DirectX:

When I make a call to some DX thing, alot of the time I''ll have to pass it some flags. These flags are just enums or defines, so all they do is stand for a number. The thing is, I can set multiple flags in the same parameter. How?
Written like this: Func( DX_FOO1 / DX_FOO2 / DX_FOO3 );
(I hate this, the message board kills the pipe key //// see?)
In the enum/define:
DX_FOO1 = 1 binary here
DX_FOO2 = 10
DX_FOO3 = 100

What the or function does (the pipe key that won''t display) it says that 1 / 10 / 100 = 111 (MAke sense?)

Three flags are set all at the same time in the same parameter.
DX would then use & to determine which flags are set.
Say it somes to the point when it wants to know if DX_FOO2 is set.
DX_FOO2 = 10, so it gets the parameter and uses the and function.
111 & 10 = 010
That isn''t zero, so the flag must be set. They are pretty much the only uses for them I''ve come across. Hope that helped.

- IO Fission

Tearing 'em apart for no particular reason...

Share this post

Link to post
Share on other sites
I just posted an explanation about them (which I don't don't garuntee to be 100% correct) on this forum in relation to checking/setting/zeroing specific bits. It is called next question: bitfields or you can just click here

They can be incredibly useful. To store a boolean value to disk without manipulating bits would take 8 bits. Now say you had 10 million boolean values. That is a signifigant amount of waste. Even for any variables that don't need to take up a whole byte or need more than a byte but not as much as a word, etc. Another use, as IO Fission mentioned, is to store more than one value in a variable. Especially useful for returning values (of course, there are ways around that). Most use found today is used in hardware drivers, where bits represents certain states of the hardware.

Edited by - Sheltem on June 17, 2000 5:45:45 AM

Share this post

Link to post
Share on other sites
blue-lightning: actually BOOL is defined DWORD or int? but anyway, not because all computers work faster with 4-byte integers, but because the target platform is a 32-bit processor. if the processor is 8, 16 or 64-bit then it would make sense to use data on that alignment, as it makes the most efficient use of the bus.

Share this post

Link to post
Share on other sites