# left shift question

## Recommended Posts

ok, how does this work int a = 1; int b = 31; int c = a << b; please explain how c works. thanks.

##### Share on other sites
How well do you know binary?

These are the values...
a0000-0000-0000-0000-0000-0000-0000-0001c1000-0000-0000-0000-0000-0000-0000-0000|----------------31 = b----------------|

##### Share on other sites
Quote:
 Original post by rip-offHow well do you know binary?These are the values...a0000-0000-0000-0000-0000-0000-0000-0001c1000-0000-0000-0000-0000-0000-0000-0000|----------------31 = b----------------|

thanks

##### 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 00000001And 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 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 on other sites
Quote:
 Original post by 31337noobok, how does this workint 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 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:10011000Left Logical Shift twice:01100000Right Logical Shift twice:00100110Left Arithmatic Shift twice:01100000Right 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 on other sites
Quote:
 Original post by nobodynewsLeft 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 on other sites
whoopsie, I'll change it.

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627714
• Total Posts
2978775

• 9
• 21
• 14
• 12
• 42