Can't understand C program output -


i making basic programs , made program

 #include<stdio.>  int main()  {    printf("%d\n",-1>>4);    return 0;  } 

output = -1

i not understand how happens ?

is -1 2's complemented first , shift operation done .and again 2's complement done produce result.

i want know how output comes

int main() {  unsigned int a=4;  printf("%d\n",-a>>4);  return 0;    } 

result = 268435455

for start, you're doing non-portable.

iso c11 states, in 6.5.7 bitwise shift operators:

the result of e1 >> e2 e1 right-shifted e2 bit positions. if e1 has unsigned type or if e1 has signed type , nonnegative value, value of result integral part of quotient of e1 / 2^e2 . if e1 has signed type , negative value, resulting value implementation-defined.

so it's not wise that.

what's possibly happening have implementation preserves sign on negative values. whereas >> may shift in 0 bits on left hand side (and must unsigned or signed-nonnegative value), implementation detect negative number , shifts in one-bits second-from-left position, leaving left bit untouched.

that means -1 (eg: binary 1111 1111 1111 1111) still have bit pattern after right-shift.

you have examine documentation particular implementation sure (appendix j of standard requires implementations document choices implementation-defined behaviours).

you test few better sample values binary 1100 0000 0000 0000 right-shifted 1 bit, , see comes out (though, of course, implementation notes should definitive source).


by way of example, gcc documentation provides information here. since mention you're using 4.6.3, 4.6.4 manuals closest.

the gcc 4.6.4 manual (also in pdf or postscript or html tarball) link on page contains section entitled c implementation-defined behaviour states, in part:

the results of bitwise operations on signed integers (c90 6.3, c99 6.5).

bitwise operators act on representation of value including both sign , value bits, sign bit considered above highest-value value bit. signed ‘>>’ acts on negative numbers sign extension.

that means acts explained, left bit staying put and affecting second-most left bit:

enter image description here


the reason why you're seeing different value with:

unsigned int a=4; printf("%d\n",-a>>4); 

is because -a is, reason i'm not entirely of, being treated unsigned representation of -4. can see with:

#include <stdio.h> int main() {     unsigned int a=4;     printf("%09x\n",((unsigned int)(-a))>>1);     printf("%09x\n",(-a)>>1);     printf("%09x\n",(-((int)a))>>1);     return 0; } 

which outputs (annotated):

07ffffffe  # explicit unsigned int 07ffffffe  # seemingly treated unsigned int  0fffffffe  # explicit int 

i suspect has integer promotions , usual arithmetic conversions detailed in iso c11 6.5 expressions think that's moving beyond scope of original question.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -