Skip to content
systemcoding.dev
Fundamentals

Bits, Nibbles, Bytes and Words: The Units of Data

Learn what a bit, nibble, byte, word and doubleword are, why a word has no single size, and how C reports the real sizes of its types on your platform.

6 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Zig and Python logos.

Programmers talk about bits, bytes and words as if the terms were fixed, but only two of them are. A bit is always one binary digit and a byte is, in practice, always eight bits. A word is whatever the machine says it is, and the term means different sizes in different manuals. This post defines each unit, explains why the word is slippery, shows the names that vendors use for 16-, 32- and 64-bit quantities, and ends with a C program that asks the compiler how big the types are. The program targets GCC 14 on x86-64 Linux with glibc 2.40, which uses the LP64 data model, and the post points out where other platforms differ.

The bit

A bit is the smallest unit of information: a single digit that is 0 or 1. Hardware stores it as one of two physical states, such as a charged or discharged cell, or a high or low voltage. One bit can answer one yes-or-no question, which is why bits are the natural size for flags.

A lone bit is awkward to address. Memory is not wired so that the CPU can fetch a single bit by address. The smallest unit that has its own address is the byte, and software that wants one bit reads a byte and masks out the others, a technique covered in the posts on bit manipulation.

The nibble

A nibble, spelled also nybble, is four bits. It is not a unit that hardware addresses, but it matters because one hexadecimal digit is exactly one nibble. A byte therefore has a high nibble and a low nibble, and they are the two hex digits you see in a dump. In the byte 0xA7, the high nibble is A and the low nibble is 7. Binary-coded decimal, a format that stores one decimal digit per nibble, is one place where the nibble is a real unit of storage.

The byte

A byte is the smallest addressable unit of memory. On every mainstream platform in use today, it is eight bits. The word octet means exactly eight bits and is used in network standards, such as the RFCs, precisely because historical machines had bytes of other sizes: six, seven and nine bits all existed.

C is careful about this. The C standard defines a byte as the unit of storage that holds a character, requires it to have at least eight bits, and exposes the real count as the macro CHAR_BIT in limits.h. POSIX goes further and requires CHAR_BIT to be 8. Embedded compilers for some digital signal processors still define a 16-bit char, so portable code that parses a wire format should not assume eight without checking. For all code that targets general-purpose systems, eight is safe.

An eight-bit byte holds 256 patterns, which is enough for an unsigned value from 0 to 255 or a signed value from minus 128 to 127. It is also large enough for a character in the ASCII range and for one colour channel of a pixel.

The word

A word is the natural unit of data for a given processor. It is typically the width of the general-purpose registers and of the arithmetic unit, and often of the data bus or the instruction encoding. On a 16-bit processor a word is 16 bits, on a 32-bit one it is 32 bits, and on a 64-bit one it is 64 bits.

The trouble is that software and documentation freeze the word at the size it had when a convention was set. The x86 family began as a 16-bit architecture, and the x86 and Windows documentation still calls a 16-bit quantity a word, even though the registers are now 64 bits wide. The same documentation then needs a doubleword for 32 bits and a quadword for 64 bits. Other architectures, such as the ARM and RISC-V manuals, use word for 32 bits for a similar reason. In ARM terminology a halfword is 16 bits, a word is 32 bits and a doubleword is 64 bits.

NameSizeWhere you see the name
bit1 biteverywhere
nibble4 bitshex digits, BCD
byte8 bitseverywhere, the addressable unit
word (x86 and Windows)16 bitsWORD in the Windows API, dw in some assemblers
doubleword (x86 and Windows)32 bitsDWORD, dd
quadword (x86 and Windows)64 bitsQWORD, dq
word (ARM, RISC-V)32 bits.word in assembly
halfword (ARM, RISC-V)16 bits.hword or .half

The practical advice is simple. When you document or discuss a quantity, say the width in bits: a 32-bit value, not a word. When you read a manual, check its glossary, since the manual defines the word for its own architecture.

The units in C

C does not use the word word for a type. It offers char, short, int, long and long long whose sizes vary by platform, and the exact-width types int8_t, int16_t, int32_t and int64_t in stdint.h for when the width must be fixed. The post on integer sizes covers the choice in detail. Here the goal is to see the sizes as the compiler reports them. The sizeof operator yields a size in bytes, and multiplying by CHAR_BIT gives bits.

sizes.cc
#include <limits.h>
#include <stdint.h>
#include <stdio.h>

int main(void)
{
    printf("CHAR_BIT        = %d\n", CHAR_BIT);
    printf("sizeof(char)    = %zu\n", sizeof(char));
    printf("sizeof(short)   = %zu\n", sizeof(short));
    printf("sizeof(int)     = %zu\n", sizeof(int));
    printf("sizeof(long)    = %zu\n", sizeof(long));
    printf("sizeof(long long) = %zu\n", sizeof(long long));
    printf("sizeof(void *)  = %zu\n", sizeof(void *));
    printf("sizeof(int32_t) = %zu\n", sizeof(int32_t));
    printf("bits in an int  = %zu\n", sizeof(int) * CHAR_BIT);
    return 0;
}

On x86-64 Linux with GCC 14 the data model is LP64, meaning long and pointers are 64 bits, and int stays at 32 bits.

Text
CHAR_BIT        = 8
sizeof(char)    = 1
sizeof(short)   = 2
sizeof(int)     = 4
sizeof(long)    = 8
sizeof(long long) = 8
sizeof(void *)  = 8
sizeof(int32_t) = 4
bits in an int  = 32

Two points are worth noticing. First, sizeof(char) is 1 by definition on every platform, because sizeof counts in units of char. It is the byte of the C abstract machine. Second, the numbers change between systems. On 64-bit Windows, which uses the LLP64 model, long is 32 bits and only long long and pointers are 64 bits. On a 32-bit Linux system, long and pointers are both 32 bits. That variation is why code that stores a pointer in a long or assumes int is 32 bits breaks when it moves. The standard guarantees only minimums and an ordering: char is at least 8 bits, short and int at least 16, long at least 32 and long long at least 64.

Bytes of data you can count

Because a byte is the smallest addressable unit, sizes of files, buffers and structures are counted in bytes. A 32-bit integer is 4 bytes and a 64-bit pointer is 8. An IPv4 address is 4 bytes, an IPv6 address is 16 bytes and a UUID is 16 bytes. A memory page on typical x86-64 Linux systems is 4096 bytes, which is 4 KiB, and the post on kilobytes versus kibibytes explains that prefix.

Knowing the byte count helps when you reason about layouts, as in network protocols and file formats. A header that says "length: 2 bytes" holds a 16-bit value, with a maximum of 65535 if it is unsigned, and a 4-byte timestamp field that counts seconds since 1970 as a signed 32-bit integer runs out in January 2038.

Common mistakes

  • Using word as a size without saying which architecture's word. Say 16-bit, 32-bit or 64-bit.

  • Assuming sizeof(long) is 4 or 8 everywhere. It is 8 on LP64 and 4 on LLP64 and on 32-bit systems.

  • Confusing a byte, an octet and a character. A byte is the addressable unit, an octet is eight bits, and a character in UTF-8 can take one to four bytes.

  • Treating sizeof as a count of bits. It is a count of bytes, so multiply by CHAR_BIT for bits.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.