-10

I came across this C code, compiled and executed it online on an online GCC compiler and received -5 as output. I changed the values of the constants and received different results but couldn't figure out the logic behind the outputs. Please help...

#include<stdio.h>
int main()
{
   int a = -10;
   a = a>>1;
   printf("%d", a);
   return 0;
}
Griwes
  • 8,805
  • 2
  • 43
  • 70
awsume
  • 7
  • 2

8 Answers8

3

In binary notation with two's complement, a looks like this:

111..1110110

Now, a right shift is done. For signed integers it is implementation specific what value the filled bits have; GCC promises "sane behavior", that is, to do an arithmetic right shift - where the sign bit (here 1) is extended.

An arithmetic right shift on an integer is dividing that integer by 2^n (n is the shift size) - no matter the sign.

Therefore the shift produces the new value:

111..1111011

Which is -5. Flipping all bits and adding one yields 000...00101, which is 5.

A logical shift would have produced

011..1111011

which has the value 2147483643 for a 32-bit integer. Note how the value even depends on the size of the integer you performed the operation on.

Columbo
  • 60,038
  • 8
  • 155
  • 203
1

>> is right shift operator.

>>1 is shifting right by 1 bit. You can find a detailed discussion here. For the results, the right-shift is equivalent to a division by 2.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

The >> Is the right shift operator. It shifts the bits to the right .

And in this occasion ,it shifts by 1 bit.

Check here and here to study more.

Also , you can find here a calculator.

Finally , as "Alter Mann" mentioned , a bitwise right-shift will be the equivalent of integer division by 2.

George
  • 5,808
  • 15
  • 83
  • 160
1

According to the C Standard (6.5.7 Bitwise shift operators)

5 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.

it seems that your implementation promotes the sign bit..

So -10 can be represented as (for simplicity I assume that sizeof( int ) is equal to 4)

-10
11111111 11111111 11111111 11110110

10 >> 1

11111111 11111111 11111111 11111011

The last value is equal to -5. To be sure that it is equal to 5 you can get its two-complement value that is calculated as

~a + 1
a
11111111 11111111 11111111 11111011

~a

00000000 00000000 00000000 00000100

~a + 1

00000000 00000000 00000000 00000101

0101 is a binary representation of 5.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The '>>' operator is a bitwise right shift operator (http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts), which effectively does a 'divide-by-2'.

The

a = a >> 1;

statement shifts the number one bit to the right.

pthomsen
  • 34
  • 2
0

this is a bit operation a = 01010 when you make a>>1 you say to your program shift one bit to right and yoour result is 00101.

0

>> means "right shift" of the bits of an integer value. It is equivalent to "/ 2" (divide by 2).

The binary representation of 10 is 1010. The binary representation of -10 is it's 2-complement: 11110110 (assuming 8 bits). Right shifting 1 bit becomes 11111011, which is the 2-complement of 101, or 5 in decimal. So, (-10 >> 1) == -5.

Juan Cespedes
  • 1,299
  • 12
  • 27
  • Thanks a lot... Your answer was simple and to the point... The fact that u preferred using 8 bits to represent the 2-complements,etc. rather than 4 bytes making it look more sophisticated is what I appreciate the most... – awsume Sep 10 '14 at 11:18
0

-10 = 11110110

If you do a>>1 then a = -5

-5 = 11111011

If you do a>>2 then a = -3

-3 = 11111101

Hence, >> is shifting the binary number to one bit right with adding 1 at the starting

Ashok
  • 93
  • 1
  • 1
  • 9