How integers are stored in memory

oliveiratheone

Gabriel Oliveira

Posted on January 29, 2024

How integers are stored in memory

This will be a brief explanation of how integers are stored in memory, but let's get started.

The storage of integers in memory depends on various factors, including the programming language, hardware architecture, and the specific type of integer (e.g, 8-bit, 16-bit, 32-bit, 64-bit).

First, let's understand what's the difference between these types of integers (8-bit, 16-bit, etc...).

This difference refers to the number of bits used to represent each integer value.

Okay, but what is a bit?

A Bit is the smallest unit of information that can be stored or transmitted in a computer, a bit can only take 2 values 0 and 1.

Now let's think what a 8-bit integer means.

This type of integer can take values between 0 and 255 because this is only the values that can be represented with 8 bits.

00000000 -> 11111111

One thing to keep in mind is that is about Unsigned Integers.

Okay, again, WTF is this?

Unsigned Integers means that all the bits are used to represent values, without reserving one bit to the signal (positive or negative), so signed integers are the opposite.

Which values 8-bit signed integer can take?

Let's think, if we lose one bit to store the value, we lost some numbers.

8-bit signed integers can store numbers between -128 -> 127.

// in rust 'u8' type means unsigned integer of 8 bits

let number: u8 = 0; // ok
let number: u8 = 255; // ok
let number: u8 = -1; // not okay
Enter fullscreen mode Exit fullscreen mode

I believe that by understanding these concepts, such as signed and unsigned integers and why there are 8-bit, 16-bit... integers, is a good basis for understanding how computers store these values.

Thanks for reading this far and bye! keep studying!

💖 💪 🙅 🚩
oliveiratheone
Gabriel Oliveira

Posted on January 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related