2011年1月3日 星期一

/ 2 and >> 1


#include <stdio.h>
int main()
{
  int i = -13;
  if ((i / 2) == (i >> 1))
    printf("yes\n");
  else
    printf("no\n");
  return 0;
}

The output is no.


A > 0 and A < -1

int main()
{
  unsigned int ui = 2147483647;
  if (ui + 1 > 0)
    printf("ui + 1 > 0\n"); 
  if (ui + 1 < -1)
    printf("ui + 1 < -1\n");
  return 0;
}
The output.
C:\SVN\book\C>a.exe
ui + 1 > 0
ui + 1 < -1

A < 0, B > 0, but B > A is wrong?

A < 0, B > 0, but B > A is wrong?

int main()
{
  int i = 2147483647;
  unsigned int ui = 2147483647;
  if (i + 1 < 0)     
    printf("i + 1 < 0\n");   
  if (ui + 1 > 0)
    printf("ui + 1 > 0\n");
  if (ui + 1 > i + 1)
    printf("ui + 1 > i + 1\n");
  return 0;
}
The output.
C:\SVN\book\C>a.exe
i + 1 < 0
ui + 1 > 0