Bit shifting

Posts 113 of 13 · Page 1 of 1
Bit shifting
Hi. I've been lookin' for a while and havn't found a real example on why bit shifting would be used. I don't really understand completely how it works. If anyone would be kind enough to explain when and why it would be used and if you feel nice enough maybe you could give me a little example.

Thank you very much.
~David
I think why knows assembly, or you could try B1ackAnge1(he knows everything)
I dont even know what bif shitting is
Not sure about Why but I know BA is brilliant when it comes to this.
I like what you did there, shifting the letters around. Hehe~
Quote Originally Posted by Void View Post
Not sure about Why but I know BA is brilliant when it comes to this.
I like what you did there, shifting the letters around. Hehe~
Bitshifting has many purposes, although generally not needed when working with high-level code, sometimes you need to set\clear\get a flag.

Consider function one performs creation a new window. Do we want this window to have a frame? Should it be closable? What should it's ID be?

We have a WORD stored in memory(which is two bytes on a 32 bit system.). Consider the first byte will hold the window's ID, and the second byte will hold upto 8 flags. Note: This demonstrates bit-shifting, same can be done with using OR or XOR masks.

//Getting flags & ID
WORD buffer = 0; //Create a null dword
unsigned char bId = (char)buffer;
unsigned char bFlags = (char)buffer<<8;//Shift over 8 bits(one byte)

Setting Flags and ID
WORD buffer;
buffer |= (ID) ;
buffer |= (flags)>>8;

Or to set a given flag:
buffer |= 1<<(Offset in data holdiung flags);

Don't let the << confuse you with other classes, such as std::cout. These classes override the <<(bit-shit-left)operator to make it perform another task. By default the << bit-shifts left.
Quote Originally Posted by Void View Post
Not sure about Why but I know BA is brilliant when it comes to this.
I like what you did there, shifting the letters around. Hehe~
I don't know jack about asm, but I don know what bit shifting is, but I see BA and Jetamay beat me to it. You should also know that the division isn't perfect.

0000 1111 = 15
then shift right...
0000 0111 = 7

and 15/2 is obviously not 7 see? its pure integer division...
another case:

0000 1110 = 14
right shift...
0000 0111 = 7
14/2 does equal 7 so see this time it makes sense.

Shifting an even number right will always give you the correct answer, but shifting an odd number will lead to an error. I don't know the details... I can look it up.

I also can't remember the rules for signed variable shifting and left shifting, but its something like the same thing.
Quote Originally Posted by why06 View Post
I don't know jack about asm, but I don know what bit shifting is, but I see BA and Jetamay beat me to it. You should also know that the division isn't perfect.

0000 1111 = 15
then shift right...
0000 0111 = 7

and 15/2 is obviously not 7 see? its pure integer division...
another case:

0000 1110 = 14
right shift...
0000 0111 = 7
14/2 does equal 7 so see this time it makes sense.

Shifting an even number right will always give you the correct answer, but shifting an odd number will lead to an error. I don't know the details... I can look it up.

I also can't remember the rules for signed variable shifting and left shifting, but its something like the same thing.
Thanks Why. I think there's an instruction for shifting with precision, I think it's called SHRD and SHLD. Shifting with double precision.

Yeah I just spent my whole day learning about bit shifting.
Error? Not really That's what the 'Carry Flag' is for - to let yo uknow you have a remainder of '1'
The whole idea of bit shifting is a bit confusing =\
Thanks Jetamay.

Isn't 1 byte 8 bits?
normallly Bit-shifting (not doing a rotate-through-carry etc) is nothing more than multiplying or dividing by multiples of 2

Shift Left once = multiple by 2
Shift Left 2 positions = multiple by 4 etc etc
Shift Right once = divide by 2

forinstance: 00000101 (5 decimal)
Shift once: 00001010 (10 decimal)
shift twice: 00010100 (20 decimal)
and ofcourse same goes in reverse

Back in the 'old' days this was SIGNIFICANTLY faster than actually doing a normal multiplication.
Quote Originally Posted by Void View Post
The whole idea of bit shifting is a bit confusing =\
Thanks Jetamay.

Isn't 1 byte 8 bits?
Yeap it is, mah bad. For some reason I mixed up one byte for flags & id, with half byte of each(which was my original example until I noticed it was unrealistic. Fixed)

All you'll probably be using bit operators for is setting, getting, and dividing information from flags. Also, I did use bit-shifting operators in JetaOs to divide segments of a virtual memory address to lcoate it's actual page.

Sorry for short replies, I'm installing my development environment again, and it takes hours of attended installation.
Quote Originally Posted by radnomguywfq3 View Post
Yeap it is, mah bad. For some reason I mixed up one byte for flags & id, with half byte of each(which was my original example until I noticed it was unrealistic. Fixed)

All you'll probably be using bit operators for is setting, getting, and dividing information from flags. Also, I did use bit-shifting operators in JetaOs to divide segments of a virtual memory address to lcoate it's actual page.

Sorry for short replies, I'm installing my development environment again, and it takes hours of attended installation.
What is your dev environment composed of to take hours?
So, bit shifting is actually used for dividing and muliplying?
Is there anything else we can make use for bit shifting?
Thanks again Jetamay. And yeah, I saw your JetaOs but I have absolutely no idea what any of it means. You guys are way too smart. =\
Posts 113 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?