Chapter 1 – Programming language: C++ – Part 1

Memory address

Before we get into C++, we need to understand the basics – the bit.

A bit is the smallest unit of storage, capable of storing only 0 and 1.

A byte is the smallest addressable unit of memory in a computer system.

As humans, we work with decimal numbers, which consists of digits from 0 to 9. Once we reach 9 and want to add 1 to it, we carry 1 and go back to 0, ending with digit 10.

A number in base 2 consist of digits of 0 and 1 only, so once we add 1 to another 1, we will not have 2, but carry 1 and go back to 0, ending with 10.

Variable types

These are only a few of C++ types.

string is actually an object, and have specific functions that can bee called, such as .size() to retrieve the number of characters.

We can also have actual arrays in the code, when we want to gather more variables under the same name.

If we want to access the actual variable, we can use the bracket operator [], for both reading from it and setting a value to it.

Signedness

Each variable occur some space in memory. Based on its type, it can be less space, or more space.

Assuming we have a type that requires 1 byte (8 bits) of memory, its lowest value is 0000 0000 (0 decimal) whereas its highest is 1111 1111 (255 decimal). This is the case for unsigned decimals (positive values only).

An integer uses 4 bytes, and it goes from -2M to +2M.

4 bytes consist of 4×8 bits => 32 bits.

Overflow occurs when we add 1 over the highest possible number that value can hold.

Let’s take the number 255 (8 bits – 1111 1111) and try to add 1 to it.

We’ll always set the digit to 0 and carry 1 until the end, where it will fall due to not having extra space.

Therefore, 255 + 1 =>

1111 1111 + 1 =>

1 0000 0000

Since we have only 8 bits to store the result, we’re left with 0000 0000, which is the lowest possible number the value can hold.

Underflow has the same effect when we decrement by 1 tthe lowest possible value, ending with the highestttt possible value.

0000 0000 – 1 =>

1111 1111 => 255

You may also like...