0x1 notes
Short notes on bits, bytes and the tools that show them.
Setting, clearing and testing a bit
Everything starts from 1 << n — a mask with only bit n set.
flags |= (1 << n); // set
flags &= ~(1 << n); // clear
flags ^= (1 << n); // toggle
(flags >> n) & 1 // test
Use an unsigned type: shifting a 1 into the sign bit of a signed int is undefined behaviour in C.
Reading a hexdump quickly
xxd and hexdump -C show the same thing: offset, bytes, printable characters.
$ printf 'PNG\r\n' | xxd
00000000: 504e 470d 0a PNG..
Two hex digits per byte, so a 16-byte row is exactly one line. The right column turns non-printable bytes into dots.
Byte order in one example
The 32-bit value 0x12345678 in memory:
big-endian: 12 34 56 78
little-endian: 78 56 34 12
x86 and most ARM systems are little-endian; network protocols are big-endian, hence htonl() and friends.