Naruto
10/3/2017 - 11:23 PM

bitshift.c

// cc -o bitshift bitshift.c

/*
result on macOS/x64

shift: 0
a: 0x00000000000000ff
b: 0x00000000000000ff
c: 0x00000000000000ff

shift: 8
a: 0x000000000000ff00
b: 0x000000000000ff00
c: 0x000000000000ff00

shift: 16
a: 0x0000000000ff0000
b: 0x0000000000ff0000
c: 0x0000000000ff0000

shift: 24
a: 0xffffffffff000000
b: 0x00000000ff000000
c: 0x00000000ff000000

shift: 32
a: 0x00000000000000ff
b: 0x000000ff00000000
c: 0x000000ff00000000

shift: 40
a: 0x000000000000ff00
b: 0x0000ff0000000000
c: 0x0000ff0000000000

shift: 48
a: 0x0000000000ff0000
b: 0x00ff000000000000
c: 0x00ff000000000000

shift: 56
a: 0xffffffffff000000
b: 0xff00000000000000
c: 0xff00000000000000
*/

#include <stdio.h>
#include <stdint.h>

int main() {
  for(int i=0; i < 8; i++) {
    int shift = 8 * i;
    uint64_t a = 0x00000000000000ff    << shift;
    uint64_t b = 0x00000000000000ffULL << shift;
    uint64_t c = (uint64_t)(0x00000000000000ff) << shift;
    printf("shift: %d\n", shift);
    printf("a: 0x%016llx\n", a);
    printf("b: 0x%016llx\n", b);
    printf("c: 0x%016llx\n", c);
    printf("\n");
  }
  return 0;
}