Day-3 C++ Basic to Advance

Day-3 C++ Basic to Advance

What I did on Day 3

On Day 3, I learned how data is stored, how data is converted from one data type to another by TypeCasting and Max and Min values that we can store in each data type.

How Data is Stored?

In C++, every value is stored somewhere in memory and can therefore be identified with that address.

How Positive numbers are stored?

Example of int

We will understand how data is stored by taking an example of int:-let's take

int a = 8 ;

Since we already know that int occupies 4 bytes, a memory of 4 bytes will be allocated to a.

so 4 bytes --> (We are considering that these are 4 bytes allocated to this variable a )

now we will convert a = 8 into binary which is 1000 but this is only 4 bits and our memory has 32 bits (4 bytes X 8 = 32 bits). So it will store 1000 in the end and 0 in the remaining part.

so we will have it like this in the memory at the end:-

00000000 00000000 00000000 00001000

And this is how integers are stored in the memory.

Similarly if b = 5, 5 is 101 in binary

so it will be stored like this:-

00000000 00000000 00000000 00000101

I hope this concept is clear to you till this point.

Example of char

Now you will be wondering how characters are stored.

So let's take an example:-

char ch = 'a' ;

For this, we have to look for the ASCII table.

File:ASCII-Table-wide.svg - Wikimedia Commons

In C++, all the characters are mapped to an ASCII table. For eg . 'a' is mapped to 97.

So what we will do, we will convert this mapped number 97 to the binary and then will store it in the memory. it is as simple as that.

Char occupies 1 byte which means it has only 8 bits. 97 in binary is 1100001. So char 'a' will be stored as:-

01100001

Type Casting

A type cast is basically a conversion from one type to another.

Let's take another example for this:-

What if we code this


#include<iostream>
usning namespace std;

int main()
{
    int a = 'a';
    cout << a << endl;
}

This will give output as 97 because according to the ASCII table char 'a' has 97 as its value and here the data type is int, so it will get typecast into the numeric 97 as a can hold only numeric value because of its data type (int).

Here is one more example like this:-

#include<iostream>
using namespace std;

int main()
{
    int a ='a'; //this will give 97 as output
    cout <<a<<endl;

    char b=98; //this will give b as output
    cout<<b; 
}

The output of this will be:-

We can see that the compiler printed the value of char b as 'b' because we have assigned 98 as the value of char b and since it is a char data type, it could only store a character as its value and 98 in the ASCII table is mapped to b, so the output is b instead of 98.

Max and Min value that we can store

Int

Let's start with data type int. Now int occupies 4 bytes which means it contains 32 bits, So the Minimum Value that we can store in our memory block will be 0.

So now we have 0 as the minimum value and the maximum value that we can store is 2^32-1 (the min value 0 is subtracted )

Char

Similarly, char occupies 1 byte i.e. 8 bytes so the Minimum Value that we can store is 0 and the Maximum value that we can store is 2^8-1.

How Negative Numbers are stored?

Till now, we know how positive numbers are stored, we know about type casting and the max and min values that could be stored in a data type.

Now comes the interesting part that how negative numbers are stored.

It is very simple if we have a positive number, the first bit will be 0 and if we have a negative number, the first bit will be 1.

Now there are a few steps that one needs to follow while storing a negative number.

Let's say the number is -5.

First ignore the negative sign and convert it into binary representation. (Since we took -5, the binary representation of 5 after ignoring the sign is 101)

so it will be stored as:-

00000000 00000000 00000000 00000101

Now take 2's compliment and store it. Now you will be wondering what is 2s compliment. Let's get familiar with this by taking the above bits as an example.

so first we will take 1's compliment of it and just change the bits 0 to 1 and 1 to 0 to find the 1s compliment

so 1s compliment will be:-

11111111 11111111 11111111 11111010

Now to find 2's compliment, we just have to add 1 to 1's compliment

11111111 11111111 11111111 11111010

+1

So the 2's compliment will be:-

11111111 11111111 11111111 11111011

Now the most significant bit (the first bit in the starting is 1 ) signifies the negative sign and in this way, -5 will be stored in the memory

End of blog

So this brings us to the end of the blog, Hope everything that I mentioned above is clear to you. Please like this article and Lemme know your thoughts or any suggestion in the comments. Your feedback would be appreciated.

🌟Resources🌟

CodeHelp-by Babbar

GeeksforGeeks

Did you find this article valuable?

Support Jaival Bhatia by becoming a sponsor. Any amount is appreciated!