Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain why '>>2' shift means 'divided by 4' in C codes?

Tags:

c

divide

shift

I know and understand the result.

For example:

<br>
 7 (decimal) = 00000111 (binary) <br>

and 7 >> 2 = 00000001 (binary) <br>
00000001 (binary) is same as 7 / 4 = 1 <br>
So 7 >> 2 = 7 / 4 <br>
<br>

But I'd like to know how this logic was created.
Can anyone elaborate on this logic?
(Maybe it just popped up in a genius' head?)

And are there any other similar logics like this ?

like image 479
user573566 Avatar asked Nov 27 '12 03:11

user573566


People also ask

Why does right shift divide by 2?

When shifting right with a logical right shift, the least-significant bit is lost and a 0 is inserted on the other end. For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.

Why does Left Shift multiply by 2?

The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the left doubles the number, therefore each left shift multiplies the original number by 2.

How does the shift operator work in C?

It is a binary operator that requires two operands to shift or move the position of the bits to the left side and add zeroes to the empty space created at the right side after shifting the bits.

Is Right Shift the same as dividing by 2?

Just as left shifts are equivalent to multiplying a number by 2, right shifts are equivalent to dividing a number by 2. However, when we shift bits to the right, a 1 in the sign bit can represent a larger positive number rather than a smaller negative number.


2 Answers

Elaborating on Aniket Inge's answer:

Number: 30710 = 1001100112

How multiply by 10 works in decimal system

10 * (30710)

= 10 * (3*102 + 7*100)

= 3*102+1 + 7*100+1

= 3*103 + 7*101

= 307010

= 30710 << 1

Similarly multiply by 2 in binary,

2 * (1001100112)

= 2 * (1*28 + 1*25 + 1*24 + 1*21 1*20)

= 1*28+1 + 1*25+1 + 1*24+1 + 1*21+1 1*20+1

= 1*29 + 1*26 + 1*25 + 1*22 + 1*21

= 10011001102

= 1001100112 << 1

like image 199
Shishir Gupta Avatar answered Oct 14 '22 11:10

Shishir Gupta


It didn't "pop-up" in a genius' head. Right shifting binary numbers would divide a number by 2 and left shifting the numbers would multiply it by 2. This is because 10 is 2 in binary. Multiplying a number by 10(be it binary or decimal or hexadecimal) appends a 0 to the number(which is effectively left shifting). Similarly, dividing by 10(or 2) removes a binary digit from the number(effectively right shifting). This is how the logic really works.

There are plenty of such bit-twiddlery(a word I invented a minute ago) in computer world.

http://graphics.stanford.edu/~seander/bithacks.html Here is for the starters.

This is my favorite book: http://www.amazon.com/Hackers-Delight-Edition-Henry-Warren/dp/0321842685/ref=dp_ob_image_bk on bit-twiddlery.

like image 35
Aniket Inge Avatar answered Oct 14 '22 12:10

Aniket Inge