8
    -5 / 2 = -2

    -5 >> 1 = -3

I learn from my teacher that >>1 divides the number by 2. It works on positive number but it does not work on negative numbers. Can someone explain to me??

Thanks

Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • It's implementation dependent. But it usually rounds down (towards negative infinity). – Mysticial Dec 13 '12 at 11:16
  • 1
    is this a possible duplicate of [Shift operator in C](http://stackoverflow.com/q/7622/1025391) ? – moooeeeep Dec 13 '12 at 11:24
  • It all depends how the number is represented in binary, ever heard of Two's Complement? It might be like that. http://en.wikipedia.org/wiki/Two's_complement – Jodrell Dec 13 '12 at 11:26
  • The shift operator moves all the bits one way or the other, because the binary represention is base 2, each bit represents a succesive power of 2. – Jodrell Dec 13 '12 at 11:30
  • your teacher told you right thing.But you misinterpret it. Again come to understanding just go for bit wise operation simply. – rajesh6115 Dec 13 '12 at 11:43

7 Answers7

6

As BЈовић & mystical states, using bit shift operators on negative numbers is implementation defined.
The reason for this is C doesn't distinguish between logical and arithmetic bit shifting.
(Arithmetic pads with the most significant bit, logical pads with 0's)
for positive numbers this doesn't matter, for both arithmetic and logical bit shifts would keep the most significant bit as a 0:
Arithmetic 5>>1
0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2

Logical 5>>1
0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2

however with a negative number (2's comp)
Arithmetic -5>>1
1111 1111 1111 1011 = -5
to
1111 1111 1111 1101 = -3

Logical -5>>1
1111 1111 1111 1011 = -5
to
0111 1111 1111 1101 = 32,765

or at least, this is how i understand it

Daboyzuk
  • 449
  • 4
  • 14
  • I don't know about mathematical and logical thing but in C whenever you use a signed integer one thing come into picture is sign bit copy. Which is MSB of every integer. – rajesh6115 Dec 13 '12 at 11:48
  • @Davoyzuk why am i getting -3 instead of 32765 – Computernerd Dec 13 '12 at 12:33
  • @Lim Sorry, it was my mistake on the arithmetic -5>>1. `1111 1111 1111 1101` is -3, not -2. The result you get in the original question is the arithmetic bit shift. The Logical bit shift is the reason why you shouldn't use the >> bit shift on negative numbers. my post has been edited to fix the mistake – Daboyzuk Dec 13 '12 at 14:07
5

It works on positive number but it does not work on negative numbers.

Using shift operator on negative integer numbers is implementation defined.


[expr.shift]/3 tells this :

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 2
    Wrong, it's implementation-defined. Not undefined. – Mysticial Dec 13 '12 at 11:17
  • why is it undefined behaviour?? – Computernerd Dec 13 '12 at 11:17
  • what the difference between implementation-defined and undefined behaviour? – Computernerd Dec 13 '12 at 11:19
  • 1
    @Lim Undefined means anything is allowed to happen. Implementation-defined means the compiler must pick a consistent behavior. – Mysticial Dec 13 '12 at 11:20
  • @BЈовић the paragraph is irrelevant to the question ( << vs >> ) – Luchian Grigore Dec 13 '12 at 11:21
  • The excerpt posted is relevant for left shift. What has is right shift. `The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.` is the part related to right shift. – P.P Dec 13 '12 at 11:22
1

I learn from my teacher that >>1 divides the number by 2.

It doesn't divide the integer by two, but it performs (depending on the value) a logical or an arithmetic shift by one bit to the right. It happens to be equal to a division by two under some circumstances.

It works on positive number but it does not work on negative numbers.

It works in both cases, but the exact behavior is not mandated by the standard, but rather implementation-defined. It usually divides by two and truncates the result towards negative infinity, in constrast to towards zero as a normal division would do.

For reference:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
1

First of all,
5 in binary is 0000 0000 0000 0101 but what about -5 ? Here it is :

  1. Change 1 to 0 and 0 to 1 ,then we get 1111 1111 1111 1010
  2. Then take that number + 1 , we get 1111 1111 1111 1011

Now we get: -5= 1111 1111 1111 1011 ( it's in 2's complement form)
So here is how to calculate -5>>1 :

  1. Move each bit of -5 from left to right ( >> ) we get 111 1111 1111 1101 (only 15 bits left)
  2. Because -5 is negative number, so we have to fill '1' to the first bit to make it become 16 bits Then we get 1111 1111 1111 1101 ( it's still in 2's complement form)
  3. Now convert it to a normal binary form by change 0 to 1 , 1 to 0 (except the first bit because it defines negative number in 2's complement), then plus '1' . so we get 1000 0000 0000 0011 = -3
ismail
  • 46,010
  • 9
  • 86
  • 95
SKRUY
  • 69
  • 13
0

I think answer is correct. As '/' (division) operator generate quotient (result of division).

In your problem :

-5/2 = -3(quotient) and 1(remainder ). 

So this is ok with both positive and negative number.

positive Number:

5/2 = 2(quotient) and 1(remainder ). 

So it is fine with positive Number.

NOTE

Remainder never be a negative number. It is always positive Number.

rajesh6115
  • 705
  • 9
  • 21
0

I guess the answer to -5>>1 = -3. In case of a positive number, say 5, division by 2 gives 2.5 rounding off to the nearest smallest integer i.e. 2

But when we consider a negative number, -5, division by 2 gives -2.5. Its rounding off to the nearest integer gives -3.

Tanu Saxena
  • 779
  • 6
  • 15
0

In c the right shift operator preserves the sign bit.hence the right shifting the bits with preserving the sign bit again yields a negative number,which is in two complements form.

prakash kumar
  • 520
  • 1
  • 5
  • 16
  • That's not 100% true, the compiler is free to do either arithmetic (preserve sign-bit) or logical (does not perserve sign-bit), as long as it picks one and sticks with it; right shift on signed types is implementation-defined. – Dennis Meng Sep 20 '13 at 22:54