0
votes

In a textbook I'm looking at, it asks me to do an arithmetic right shift to the binary sequence 0110 0100 by 3 (a >> 3). I thought it was 0000 1100 as I would add 3 zeros at the right considering the most significant bit is 0 (or is it 01?), but the answer key says the correct answer is 1110 1100.

An arithmetic shift is defined as follows:

An arithmetic right shift fills the left end with k repetitions of the most significant bit...

As the most significant bit in 0110 0100 is 0, should I not be adding 0s instead of 1s? Is the most significant bit actually 01?

As a separate example, if a bit refers to the first two digits and not only the first, why is a right shift of 3 to the binary sequence 0111 0010, 0000 1110, instead of 1110 1110?

1
Could you clarify exactly what the book says? Because you have different but sequences in title and beginning of the text and the end of the text. And a bit is one bit, not twoSami Kuhmonen
@SamiKuhmonen Reformatted!NotDuplicateSL
It should be 0000 1100, please verify again Or may be there could be a printing mistake.arun pal
0b00110100 >> 3 = 0b00000110, as far as I know ideone.com/Nysq4fgeckos
Yup right shift by 3 will be 0000 1000.arun pal

1 Answers

0
votes
Index : 76543210
a >> 0: 01100100
a >> 1: 00110010
a >> 1: 00011001
a >> 1: 00001100

Another example:

Index : 76543210
a >> 0: 10000100
a >> 1: 11000010
a >> 1: 11100001
a >> 1: 11110000

It is exactly as you believe it to be. An arithmetic shift, otherwise known as a signed shift, which already gives you a good hint at what it does, shifts the bits left or right, while preserving the signed bit and when moving right, replicates the signed bit.

Here is the formal definition:

A shift, applied to the representation of a number in a fixed radix numeration system and in a fixed-point representation system, and in which only the characters representing the fixed-point part of the number are moved. An arithmetic shift is usually equivalent to multiplying the number by a positive or a negative integral power of the radix, except for the effect of any rounding; compare the logical shift with the arithmetic shift, especially in the case of floating-point representation.

The Wikipedia entry has some good illustrative pictures: https://en.wikipedia.org/wiki/Arithmetic_shift

Likely the author simply messed up his own example, wanting to show that it preserves the left-most bit (in most cases) and replicates it. Though he forgot to set it.