Binary Numbers and Bits
Interactive lab
Try it: Binary Numbers and Bits
How an 8- or 16-bit pattern represents a number by place values (powers of two), how to convert by repeated division by 2 and to hexadecimal, and how addition with carries, AND / OR / XOR / NOT, shifts and two's complement work bit by bit.
How it works
- Bit i is worth 2^i; the value is the sum of the place values of the 1-bits (in two's complement the top bit is worth -2^(w-1)).
- Converting the other way: divide by 2 repeatedly; the remainders, read from last to first, are the bits. Each group of 4 bits is one hex digit.
- Addition works column by column from the right: add the two bits and the carry, write the low bit, carry the high bit. A carry out of the top bit (or a sign change) is overflow.
- AND, OR, XOR and NOT combine or flip each column independently; << and >> move every bit left or right.
- Negation in two's complement: invert every bit, then add 1.
Default run (10 steps): a + b on 8-bit patterns, read as unsigned numbers. a = 00101101 (45), b = 00011011 (27). … Done: a + b = 01001000, which reads as 72 (unsigned).
Simplified: Fixed widths of 8 or 16 bits. Python's own ints are unbounded (they never overflow and ~x is -(x+1)); the lab shows the fixed-width result and states the unbounded Python value where they differ.
Educational simulation
Loading the simulation…