1.8: A short Introduction to Binary Numbers and Computer Storage

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    1.8: A short Introduction to Binary Numbers and Computer Storage

    Before we continue learning more MATLAB commands, it is helpful to take a short detour and talk about how computers store information. This may feel a little different from writing MATLAB code, but it is an important idea in engineering and programming.

    When we write numbers in everyday life, we almost always use the decimal number system, also called base 10. The decimal system uses ten digits:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    For example, when we write the number 421, we understand it as:

    421 = 4(100) + 2(10) + 1(1)
    

    or, using powers of 10:


    \(421 = 4(10^2) + 2(10^1) + 1(10^0)\)

    This works because each digit position represents a power of 10. The digit on the far right is in the ones place, the next digit is in the tens place, the next digit is in the hundreds place, and so on.

    Computers, however, do not naturally store information using ten digits. A computer is built from electronic circuits, and at the lowest level these circuits are easiest to represent using two states: off and on. These two states are usually represented as 0 through 255.

    Because of this, computers store information using the binary number system, also called base 2.

    What Is a Binary Number?

    A binary number is a number written using only the digits 0 and 1.

    For example, the following are binary numbers:

    101
    1101
    10010
    11111111
    

    At first, binary numbers may look strange because we are so used to decimal numbers. But the idea is actually very similar. In decimal, each digit position represents a power of 10. In binary, each digit position represents a power of 2.

    For example, consider the binary number:

    1101
    

    Starting from the right side, the place values are:


    \(2^0, 2^1, 2^2, 2^3\)

    So:


    \(1101 = 1(2^3) + 1(2^2) + 0(2^1) + 1(2^0)\)

    \(1101 = 1(8) + 1(4) + 0(2) + 1(1)\)

    \(1101 = 8 + 4 + 0 + 1 = 13\)

    So the binary number 1101 is equal to the decimal number 13.

    Bits and Bytes

    Each individual 0 or 1 in a binary number is called a bit.

    The word bit comes from binary digit.

    So the binary number:

    101101
    

    has 6 bits.

    A group of 8 bits is called a byte. For example:

    01001000
    

    is one byte because it contains 8 bits.

    Bytes are important because computer memory is commonly measured using bytes. You have probably seen units such as kilobytes, megabytes, gigabytes, and terabytes. These are all based on bytes.

    A byte can store 8 binary digits. Since each bit can be either 0 or 1, the total number of different combinations that can be made with 8 bits is:


    \(2^8 = 256\)

    This means one byte can represent 256 different values.

    If we use one byte to represent only nonnegative whole numbers, the possible values are 0 through 255:

    That is why the largest unsigned integer that can be stored in 8 bits is 255.

    Why Is the Largest 8-Bit Unsigned Integer 255?

    Let’s look more carefully at why one byte can store values from 0 to 255.

    The smallest 8-bit binary number is:

    00000000
    

    This is equal to 0 in decimal.

    The largest 8-bit binary number is:

    11111111
    

    To convert this to decimal, we add the place values:


    \(11111111 = 1(2^7) + 1(2^6) + 1(2^5) + 1(2^4) + 1(2^3) + 1(2^2) + 1(2^1) + 1(2^0)\)

    \(= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255\)

    So the largest number that can be represented with 8 bits, if all values are nonnegative, is 255.

    This is why MATLAB’s uint8 data type, which means unsigned 8-bit integer, stores integer values from 0 to 255.

    Signed and Unsigned Integers

    In MATLAB, you will see integer types such as:

    • uint8
    • int8
    • uint16
    • int16
    • uint32
    • int32

    The letter u in uint8 stands for unsigned. An unsigned integer cannot be negative. It only stores zero and positive values.

    For example, a uint8 variable uses 8 bits and stores values from 0 to 255.

    A signed integer, such as int8, can store both negative and positive values. Since some of the available bit patterns must be used to represent negative numbers, the positive range becomes smaller.

    An int8 variable also uses 8 bits, but it stores values from -128 to 128.

    Notice that both uint8 and int8 use 8 bits. The difference is how MATLAB interprets those bits.

    This is an important idea: the same pattern of 0s and 1s can mean different things depending on the data type.

    MATLAB Functions: Converting Between Decimal and Binary

    MATLAB has built-in functions that can help us convert between decimal and binary numbers.

    The function dec2bin converts a decimal number to binary.

    Example \(\PageIndex{1}\)

    Coverting a decimal number to a binary number.

    dec2bin(13)

    Solution

    ans =
    
        '1101'

    This tells us that the decimal number 13 is written as 1101 in binary.

    We can also convert a binary number back to decimal using bin2dec:

    Example \(\PageIndex{2}\)

    Coverting a decimal number to a binary number.

    bin2dec('1101')

    Solution

    ans =
    
        13

    This tells us that the decimal number 13 is written as 1101 in binary.

    Notice that the binary number is placed inside quotes when using bin2dec. This is because MATLAB treats the binary number as text. If we typed 1101 without quotes, MATLAB would think we mean the decimal number one thousand one hundred one, not the binary number 1101.

    Why Should Engineering Students Care About Binary?

    You may be wondering: “If MATLAB lets me type normal decimal numbers, why do I need to know about binary?”

    That is a fair question. For most beginner MATLAB programs, you will not need to manually write binary numbers. However, binary numbers are still important because they explain how computers store and process data.

    Binary numbers show up in many engineering areas, including:

    • computer programming
    • electrical and computer engineering
    • digital logic circuits
    • microcontrollers and embedded systems
    • sensors and data acquisition
    • image processing
    • communication systems
    • robotics
    • memory usage and data storage

    For example, a sensor may send data to a microcontroller as binary values. A grayscale image may store each pixel intensity as an 8-bit number between 0 and 255. A digital output pin on a microcontroller may use 0 to represent “off” and 1 to represent “on.”

    So even though MATLAB allows us to work mostly with decimal numbers, binary is still the language of the computer underneath.


    This page titled 1.8: A short Introduction to Binary Numbers and Computer Storage was last modified on Tue, 30 Jun 2026 02:11:48 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.