Skip to content

Instantly share code, notes, and snippets.

@hnw
Created June 9, 2018 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnw/4d613f080eb01ef10d02c21c658d7bea to your computer and use it in GitHub Desktop.
Save hnw/4d613f080eb01ef10d02c21c658d7bea to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
int main()
{
long i1 = -1;
long i2 = LONG_MIN; // -9223372036854775808
unsigned long u = ULONG_MAX; // 18446744073709551615
printf("%ld\n", i1>>1); // 最上位ビット維持、算術シフト (-1)
printf("%ld\n", i1<<1); // 算術シフトでも論理シフトでも結果は同じ (-2)
printf("%ld\n", i2>>1); // 最上位ビット維持、算術シフト (-4611686018427387904)
printf("%ld\n", i2<<1); // 最上位ビット維持せず。算術シフト? (0)
printf("%lu\n", u>>1); // 論理シフト (9223372036854775807)
printf("%lu\n", u<<1); // 論理シフト (18446744073709551614)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment