Q&A-10Walk me through what a bit and a fixed-width word are, how to read a binary value by hand, and why the width constrains every operation that follows.
Answer
A bit is one binary digit, 0 or 1. A fixed-width word is a fixed count of those bits held in one machine integer (8, 32 or 64 in practice) and the width is what turns a row of bits into a number. A w-bit word has 2^w distinct patterns, and the value is the sum of the place values whose bit is set: 1, 2, 4, 8, 16 … from bit 0 upward. Bit 0 is the least significant and is drawn on the right; the left-to-right string is a display convention, not a statement about how the bytes sit in memory.
Reading by hand, take 0b1011_0101 and add the columns that are set:
| bit index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 10110101 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
128 + 32 + 16 + 4 + 1 = 181. Writing is the same walk backwards: subtract the largest place value that fits and repeat. 13 = 8 + 4 + 1, so 13 is 0b1101, with bits 3, 2 and 0 set. That round trip is the whole conversion; bin(13) and int('1101', 2) just automate what you can do on a whiteboard.
The frame changes when the word is signed. Two's complement puts the sign bit at position w-1 and reads its place value as negative: in 8 bits, 128 becomes -128. So 0b1011_0101 is 181 unsigned and -75 signed: -128 + 32 + 16 + 4 + 1 = -75, equivalently 181 - 256. To write -75 down: 75 is 0100 1011, invert to 1011 0100, add one, get 1011 0101. Same bits, two readings.
Width decides four things before any operation runs:
| w | unsigned range | signed range | sign bit |
|---|---|---|---|
| 8 | 0 .. 255 | -128 .. 127 | 7 |
| 32 | 0 .. 4 294 967 295 | -2 147 483 648 .. 2 147 483 647 | 31 |
| 64 | 0 .. 18 446 744 073 709 551 615 | -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807 | 63 |
Truncation and wraparound fall straight out of that table. 300 is 0b1_0010_1100, nine bits; forced into 8 unsigned bits the top bit goes and you get 0b0010_1100 = 44, which is 300 mod 256. In an 8-bit unsigned word 255 + 1 is 0; in an 8-bit signed word -128 - 1 is 127. And the sign bit is 7, 31 or 63 depending on width: anything hard-coding 31 is wrong the day it meets a 64-bit value.
The failure this invites is specific: assuming every language wraps at the width. Java's int is 32-bit two's complement and overflow wraps silently (JLS §4.2.1–4.2.2). C++20 mandates two's complement representation but signed integer overflow is still undefined behaviour. Python 3 ints have unlimited precision, so nothing truncates or wraps unless you ask. It survives code review because the tests were written in the language where the assumption happened to hold.
bin(13) # '0b1101'
int('1101', 2) # 13
bin(-75 & 0xFF) # '0b10110101' - an 8-bit view; Python itself will not truncateIn practice, fix width and signedness before writing the trick, put 0, 2^w - 1 and 2^w in the tests, and make the width explicit in the type wherever code crosses a serialization or language boundary. Shifts, masks and XOR then operate on a frame that is actually defined.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.