1 << n vs. 1U << n and a cell phone autofocus problem
Maybe 15 years ago, I heard that a certain cell phone camera would lose the ability to autofocus for about two weeks, then it would go back to working for another two weeks, and so on. It had something to do with the time (<some unit> since the epoch), the bits in use, and a fun little thing called sign extension.
I got some of this from a leaflet that was posted around where I worked at the time. It was posted in areas where the public could see it, so I figure it's fair game.
Here's a nice little test program to show what I'm talking about:
#include <stdio.h> static unsigned long set_bit_a(int bit) { return 1 << bit; } static unsigned long set_bit_b(int bit) { return 1U << bit; } int main() { printf("sizeof(unsigned long) here: %zd\n", sizeof(unsigned long)); for (int i = 0; i < 32; ++i) { printf("1 << %d : 0x%lx | 0x%lx\n", i, set_bit_a(i), set_bit_b(i)); } return 0; }
This does something mildly interesting when run on a 64 bit system:
$ bin/exp/signext sizeof(unsigned long) here: 8 1 << 0 : 0x1 | 0x1 1 << 1 : 0x2 | 0x2 1 << 2 : 0x4 | 0x4 1 << 3 : 0x8 | 0x8 ... 1 << 28 : 0x10000000 | 0x10000000 1 << 29 : 0x20000000 | 0x20000000 1 << 30 : 0x40000000 | 0x40000000 1 << 31 : 0xffffffff80000000 | 0x80000000
Meanwhile, the same code on a 32 bit machine is relatively boring:
$ ./t sizeof(unsigned long) here: 4 1 << 0 : 0x1 | 0x1 1 << 1 : 0x2 | 0x2 1 << 2 : 0x4 | 0x4 1 << 3 : 0x8 | 0x8 ... 1 << 28 : 0x10000000 | 0x10000000 1 << 29 : 0x20000000 | 0x20000000 1 << 30 : 0x40000000 | 0x40000000 1 << 31 : 0x80000000 | 0x80000000
Gotta love it.