Sign in to follow this  
31337noob

left shift question

Recommended Posts

How well do you know binary?

These are the values...

a
0000-0000-0000-0000-0000-0000-0000-0001

c
1000-0000-0000-0000-0000-0000-0000-0000

|----------------31 = b----------------|

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
How well do you know binary?

These are the values...

a
0000-0000-0000-0000-0000-0000-0000-0001

c
1000-0000-0000-0000-0000-0000-0000-0000

|----------------31 = b----------------|



thanks

Share this post


Link to post
Share on other sites
Well, I'm not quiet sure about what you want to know.
Basically, what that's doing is to shift the 'a' value 31 bits to the left.

So:
1 = binary: 00000000 00000000 00000000 00000001

And after shifting it will become
-2147483648 = binary: 10000000 00000000 00000000 00000000


for example, if you shift 1 (binary: 00000001) to the left by 2 bits you'll get 4 (binary: 00000100).

If you want to know how it really shifts those bits, well, x86 assembly has those instructions, so it's not manually done, the result is calculated inside the CPU.

[edit:] beaten to it... damn, I'm slow...
[edit2:] I had given a valid example, but not what I wanted xD. Anyway, it's correct now.

[Edited by - Kamikaze15 on April 16, 2006 8:31:38 PM]

Share this post


Link to post
Share on other sites
31337noob, be careful about sign extension when doing right-shifts as a right-shift on signed numbers is implementation defined. So, if you right-shifted a signed variable and the high (last) bit was set (the number is negative) then two things can happen: The high bit and preceding ones could be shifted right (keeping the number negative) or, it could fill the shifts with zero, making the number non-negative. If you're not shifting negative numbers, use unsigned.

- xeddiex

Share this post


Link to post
Share on other sites
Quote:
Original post by 31337noob
ok, how does this work

int a = 1;
int b = 31;
int c = a << b;



One way to think of shifting right and left is to imagine that you are dividing or multiplying by powers of 2 (since we are operating in binary. See Kamikaze's binary representation example to understand why).

So, shifting left by 2 is same as multiplying a number by 2^2. Right shifting a number by 2 is same as dividing by 2^2.

In the code you've given, a << b will be same as 1 * 2^31 whose result will be assigned to c.

As for understanding C you need to buy a good book and practice lots! :)


Share this post


Link to post
Share on other sites
There are actually different names for all the different kinds of shifts. The most common are Logical Shifts and Arithmatic Shifts.

A Logical shift just moves the bits around without concern for the sign.
An Arithmatic moves the bits around while taking into account the sign. Some examples(in 8 bit, because 32 bit isn't needed to get the point across)
Original value:
10011000

Left Logical Shift twice:
01100000

Right Logical Shift twice:
00100110

Left Arithmatic Shift twice:
01100000

Right Arithmatic Shift twice:
11100110


There are variations on this, see this entry of wikipedia for additional info. In C and C++ arithmatic shifts occure when shifting signed variables, and logical shifts occur when shifting unsigned variables.

Consule this entry of wikipedia for additional information
http://en.wikipedia.org/wiki/Bitwise_operation

[Edited by - nobodynews on April 17, 2006 4:44:24 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
Left Arithmatic Shift twice:
11100000


Whilst the rest of your post is correct, this one is wrong. There is no difference between a left logical shift and a left arithmetic shift.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The Windows Calc has a "Lsh" (left-shift) function. You could, you know, use it for visualization purposes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this