DYSTOPIANPIXEL

Bits, bytes and words

Understand nibbles, eight-bit values, sixteen-bit values and their high and low parts.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 1 / PART 04

Bits, nibbles, bytes and words

What is a bit?

A bit is the smallest unit we will normally talk about in this course. A single bit can contain only 0 or 1.

0
1

One bit by itself cannot represent very much. The power comes from grouping bits together.

Bit positions and their values

Inside a byte, bit positions are conventionally numbered from 7 down to 0. Bit 0 is the rightmost bit and is sometimes called the least-significant bit. Bit 7 is the leftmost bit and is the most-significant bit.

bit number:   7   6   5   4   3   2   1   0
place value: 128  64  32  16   8   4   2   1

This numbering becomes very important later because hardware registers, flags, sprite attributes and NextRegs are often documented by saying that a particular feature lives in 'bit 5' or 'bits 2–0'.

If a bit is 1, its place value contributes to the number. If it is 0, it contributes nothing. For example, %01000010 has bit 6 and bit 1 set, so its value is 64 + 2 = 66 decimal.

What is a nibble?

A nibble is a group of four bits. Four bits can represent sixteen different patterns, from 0000 through 1111. That is exactly the range represented by one hexadecimal digit, 0 through F.

1010  = one nibble
1010  = hex A

What is a byte?

A byte is eight bits. Because eight bits can form 256 different patterns, a byte can represent unsigned values from 0 through 255.

00000000 = $00 = 0
11111111 = $FF = 255

Many Z80 registers are eight bits wide, so the range $00 to $FF appears constantly.

High nibble and low nibble

Because a byte contains two nibbles, programmers sometimes refer to the left-hand four bits as the high nibble and the right-hand four bits as the low nibble.

$42 = 0100 0010

NibbleBinaryHex
High0100$4
Low0010$2

The words high and low refer to significance: the high nibble contributes the larger place values, while the low nibble contributes the smaller ones.

Why 255 is the largest unsigned value in one byte

There are eight bit positions in a byte, with values 128, 64, 32, 16, 8, 4, 2 and 1. If every bit is set to 1, the total is:

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 255

The important detail is that there are 256 possible patterns because we count from zero. The range 0 to 255 contains 256 values.

What is a 16-bit value?

Two bytes placed together give sixteen bits. Sixteen bits can represent 65,536 different patterns. For an unsigned value, that is 0 through 65,535, or $0000 through $FFFF.

The Z80 has a 16-bit address space, so addresses are naturally written as four hexadecimal digits:

$0000
$1234
$6000
$FFFF

High byte and low byte

A 16-bit value contains two bytes. The left-hand byte is called the high byte and the right-hand byte is called the low byte.

Part of $1234Value
High byte$12
Low byte$34

For $6000, the high byte is $60 and the low byte is $00. This distinction becomes important when we look at register pairs and little-endian machine code.

A note about the word 'word'

In low-level programming, the word 'word' is often used for a multi-byte value. On the Z80, programmers commonly use '16-bit value' or 'word' to mean two bytes. Because terminology can vary between processors and systems, this tutorial will usually say '16-bit value' when precision matters.

Leading zeros can show intended width

Leading zeros do not change the numerical value, but they are useful because they show the size we are thinking about.

$A   and $0A   are the same numerical value

$0A  visually shows one byte
$000A visually shows a 16-bit value

In low-level programming this visual consistency is helpful. A byte is commonly written with two hexadecimal digits and a 16-bit address with four.

For now, treat values as unsigned

The same bit pattern can later be interpreted as signed or unsigned. Tutorial 1 uses unsigned values unless it explicitly says otherwise. Signed two's-complement representation deserves its own careful treatment later.