# code explanation

drac yang

Ranch Hand

Posts: 71

Satheesh chandragupthan

Greenhorn

Posts: 4

posted 3 years ago

- 1

take look on the below explanation please..

value >> num

Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:

int a = 32;

a = a >> 2; // a now contains 8

When a value has bits that are "shifted off," those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low order bits to be lost, resulting again in a being set to 8.

int a = 35;

a = a >> 2; // a still contains 8

Looking at the same operation in binary shows more clearly how this happens:

00100011 35

>> 2

00001000 8

value >> num

Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:

int a = 32;

a = a >> 2; // a now contains 8

When a value has bits that are "shifted off," those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low order bits to be lost, resulting again in a being set to 8.

int a = 35;

a = a >> 2; // a still contains 8

Looking at the same operation in binary shows more clearly how this happens:

00100011 35

>> 2

00001000 8

posted 3 years ago

<nitpick>

Actually, that's wrong. It's the

</nitpick>

You actually go on to show the unsigned version, so I'm not sure if this was a misprint or not.

Winston

drac yang wrote:x >>= 5; means move rightward 5 bits in the unsigned way(only zero would be moved in from the leftmost bit)

<nitpick>

Actually, that's wrong. It's the

*signed*shift, so the sign bit will be moved in regardless of its value.

</nitpick>

You actually go on to show the unsigned version, so I'm not sure if this was a misprint or not.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 3 years ago

Here 'actual bit size' means, the minimum number of bits need to express the number in binary form.

Think base=2 and exp=5. Then the value is 2*2*2*2*2.

But the actual bit size of exp is 3 (bits- 101). When it is decreased by one first bit is canceled and zero is added to the beginning.(As exp must be>=0). If clause is run only 3 times.

Therefore, if there is no 'if clause', result is 2*2*2.

But the last bit of exp will be 1 two times. Therefore if clause is run twice. Therefore final answer is 2*2*2*2*2

Actually, use of >> here is to decrease bit size by one..

ten doeschate wrote:

in this

what does >>= stand for

Here 'actual bit size' means, the minimum number of bits need to express the number in binary form.

Think base=2 and exp=5. Then the value is 2*2*2*2*2.

But the actual bit size of exp is 3 (bits- 101). When it is decreased by one first bit is canceled and zero is added to the beginning.(As exp must be>=0). If clause is run only 3 times.

Therefore, if there is no 'if clause', result is 2*2*2.

But the last bit of exp will be 1 two times. Therefore if clause is run twice. Therefore final answer is 2*2*2*2*2

Actually, use of >> here is to decrease bit size by one..

Ramesh-X