c - (-1 >> 1) == -1 - Why? -


Why (- 1>> 1) result -1 < / Code>? I am working in C, although I do not think this matter should be the case.

I do not know what I'm missing ...

Here's an example: a program that casts:

  # Include & lt; Stdio.h & gt; Int main () {int num1 = -1; Int num2 = (num1 & gt; & gt; 1); Printf ("num1 =% d", num1); Printf ("\ nnum2 =% d", num2); Return 0; }  

Because the signed integers are represented in the notation.

-1 would 11111111 (if it was an 8 bit number).

-1> gt; & Gt; 1 clearly signatures so that it remains 11111111 . This behavior depends on the compiler, but when transferring a signed number to correct ( & gt; & gt; ), the sign bit is copied, whereas the correct to move an unsigned number Reason 0 to be located on the left.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -