Bits, Bytes and ASCII
Click a bit to flip it, count with the โฒ โผ arrows, or type the byte's value in decimal or hex โ the fields that changed stay marked until the next change.
Byte (8 bits)
Hex (00โFF)
ร16
ร1
Character
Description
The byte and its eight bits
- A bit is a single 0 or 1. A byte is eight bits, and it is the smallest unit a computer addresses in memory or on disk.
- Each bit carries a weight, a power of two: from the right, 1, 2, 4, 8, 16, 32, 64 and 128. The byte's value is the sum of the weights whose bits are 1.
01000001is 64 + 1 = 65.11111111is 128 + 64 + โฆ + 1 = 255, the largest value a byte holds.
- Eight bits give 2โธ = 256 combinations, so a byte holds the numbers 0 to 255.
- The leftmost bit is the MSB (most significant bit, bit 7, worth 128) and the rightmost the LSB (least significant bit, bit 0, worth 1). Flipping the MSB changes the value by 128; flipping the LSB changes it by 1 and decides whether the number is odd or even.
- Bits are numbered from the LSB, starting at 0, so bit n is worth 2โฟ.
- Counting up by 1 adds 1 at the LSB, exactly as in decimal: a bit that is 0 becomes 1 and the count stops; a bit that is 1 becomes 0 and carries 1 into the next bit to the left. So
0000 0111+ 1 flips four bits and gives0000 1000. - The โฒ and โผ arrows stop at 0 and 255. A real 8-bit register overflows instead: 255 + 1 carries out of the MSB, the carry is lost, and the byte wraps to 0.
Hexadecimal: one digit per four bits
- Hex is base 16, written with the digits
0โ9andAโF(10โ15), and usually prefixed with0x. - One hex digit covers exactly four bits, a nibble, so every byte is exactly two hex digits:
0100 0001is0x41. The high digit is the left nibble (bits 7โ4, worth 16 each) and the low digit the right nibble (bits 3โ0); changing one digit changes only its own four bits. - That is why memory dumps, colours (
#FF8800) and escape codes (\x1b) are written in hex: the bits can be read straight off the digits.
ASCII: a number for every character
- A byte stores only a number. ASCII is the agreed table saying which number means which character, so that
65on one machine isAon every other. - ASCII defines 128 characters, the numbers 0 to 127, which fit in 7 bits; the eighth bit of an ASCII byte is always 0.
- The table is laid out so that bits do useful work:
0โ31and127are control characters: instructions to a terminal or printer (new line, tab, bell) rather than symbols. The Ctrl key produces them โ Ctrl clears bit 6 (worth 64) of an uppercase letter, so Ctrl-J (Jis 74) sends 10, the line feed.48โ57are the digits0โ9: the digit's value is the byte's low four bits, so'7'is0x37.65โ90areAโZand97โ122areaโz. The two cases differ only in bit 5 (worth 32): flip it onAand you geta.
- The numbers 128 to 255 are outside ASCII. Other character sets filled them in differently โ Latin-1 put accented Latin letters there, others put Hebrew or Cyrillic โ which is why text decoded with the wrong table comes out garbled.
- UTF-8, today's standard encoding, keeps every ASCII character as the same single byte, so a plain ASCII file is already valid UTF-8. Every other character takes two to four bytes, each of them in the 128โ255 range.