Click to See Complete Forum and Search --> : bitwise operators and bit manipulation
biosx
07-09-2001, 05:02 PM
I'm an amateur coder. I've mainly just stuck with Pascal, VB, and bash shell scripts. However, I've been reading a book on C and I'm wondering if somebody could help me clarify something.
Can somebody give me an example or a good reason for using bitwise operators and manipulating bits. I've seen some examples which are nothing more than examples on how to use the operators (&, |, >>, <<, etc.).
Any help and or clarification would be much appreciated.
------------------
root is the greed of all evil
reklis
07-09-2001, 05:20 PM
See the sig :P
No, seriously, the reason is because you can't have anything smaller than a byte. If you wanted to manipulate that byte, you need to use the bitwise operators.
Let's take the old-sk00l DWORD return code for an example. If you return:
11011000000000011111111000110101
Each one of those 32 bits can stand for something. You can check these values, and manipulate them by shifting, and XOR'ing them to death.
Now, I would suggest using a struct or a class or something. Most of the times that I've seen this used is to manipulate/check data that has been returned in this form that somebody has no control over (eg: some of the PalmOS API). Another good use is cryptography, however I can't really give a good example of this. Just hit Google.
------------------
Advocate of the Sharky (Ultra) High-Resolution Club [SHRC (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)]
main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}
biosx
07-09-2001, 05:44 PM
Thank you for the reply.
Do you (or anybody else for that matter) use bitwise operators alot? It seems like you would only use them for special cases like you said (Crypto and special API programming).
Is there anytime where you regularly use bitwise operators?
Any help would be very much appreciated.
------------------
root is the greed of all evil
gammaray51
07-09-2001, 06:00 PM
Originally posted by biosx:
Thank you for the reply.
Do you (or anybody else for that matter) use bitwise operators alot? It seems like you would only use them for special cases like you said (Crypto and special API programming).
Is there anytime where you regularly use bitwise operators?
Any help would be very much appreciated.
Ive done some embedded control programming with small microprocessors that require bitwise operators. Mostly the operators are used for masking.
An example, which I used a lot, was when I started programing for the 68HC11. It has 4 ports and some of those can be both input and output ports (input denoted as a 0, output as 1). This was stored in DDRD (data data direction port D). If it was half in, half out then it would be:
DDRC = 00001111.
If you need to check which ports were in and which were out you could use the bitwise and to mask out all the other bits.
The main reason I can think of for the >> << operators is for quick divide and multiply by 2.
Ulrick28
07-09-2001, 08:51 PM
I game coding it's used A LOT...and I do mean a LOT.
For one
Shifting a bit down one takes a lot less cycles than dividing a value by 2.
ie
2>>1 = 1
Secondly, for using with flags. On console machines with limited memory you can store 32 boolean values in one U32 value instead of using a seperate u32 for each boolean value (as MSC tends to default too).
When doing compression or binary search trees you use a lot of bit comparisons/bit shifting.
digital controllers on the PC usually return all of thier values in a u32 value. So each button pressed would be a different bit in the return valued.
There are lots and lots of reasons to learn/use bitwise operations.
------------------
P4 1.4 oc'd to 1.53 (fsb 112 mhz)
Asus P4t motherboard
512mb pc800 memory
Elsa Gladiac 920 GeForce 3 (240 gpu, 540 mem)
Creative Sound Blaster Live Platinum 5.1
2 30 gig IBM deskstar drives
Promise FastTrack 100 IDE RAID controller
Sony G400 19" monitor
second machine:
Athlon 1.33ghz 266 mhz fsb
GeForce 2 Ultra
512 mb DDR 266 ram (samsung)
Creative labs Sound Blaster Live value
IBM deskstar 30gig
IBM deskstar 15gig
19" Hitachi Flat screen (CM771)
biosx
07-09-2001, 09:55 PM
Thanks for your replies, I think I understand a little better now.
Thanks again.
------------------
## root is the greed of all evil ##
/* Navi Specs */
Abit KT7-RAID
Duron 800 @ 1GHz
Geforce 2 GTS
512MB Micron/Crucial SDRAM
SB Live! 5.1 w/ Live! Drive
Maxtor ATA100 40GB
Pioneer 16x DVD
HP 9300 series CD-RW
Netgear FA311 NIC