Microcontroller Embedded C Programming Lecture 26| Range calculation for ‘char’ data type

 

Range calculation for ‘char’ data type

 

 

In this article, let’s discuss the range calculation of char data type. 

  • We know that variable of the char data type consumes 1 byte of memory.
  • Also, the char data type will be used to store 1 byte of signed data.

 

1 byte signed data representation:

Let’s see how signed data will be stored in 1 byte of computer memory. Let’s start with 1 byte signed data representation.

1 byte means 8 bits. Out of 8 bits, the most significant bit will be used to represent the sign of the data. If it is 0, then the data is considered positive. If it is 1, then the data is considered negative. And if the 7th bit or the most significant bit is 1, then the data is considered negative. And the remaining 7 bits are used to store the magnitude.

Magnitude is stored in 2’s complement format if data is negative.

Signed data means a dedicated 1 bit is used to encode a sign of the data.

Figure 1. 1 byte signed data representation
Figure 1. 1 byte signed data representation

 

Let’s see an example. Represent the data -25 in 1 byte signed data representation.

So, the data given is -25. It has two parts. One is a sign, and another one is magnitude. The sign is negative(-), and the magnitude is 25. Since the data is negative, the magnitude will be stored in 2’s complement form. You have to remember that. So, out of 8 bits, the seventh bit, that is the most significant bit, will be 1 in this case. And the remaining 7 bits are used to store the magnitude that is 25 in 2’s complement form. Let’s see how we can do that.

Figure 2. Example
Figure 2. Example

 

First of all, the data given is -25. Here let’s represent the magnitude that is 25 in binary format.

Figure 3. Calculate the 2’s complement form of -25
Figure 3. Calculate the 2’s complement form of -25

 

The binary form of 25 is 11001. 1’s complement of binary 25 is 00110.

To represent 25, you at least need 5 bits. But it is 1 byte representation, so there will be the remaining 3 bits. Let’s consider them as 0. 00011001 is a representation for 25 or +25. 

Now, let’s take the 1’s complement of this binary format. So now, it will be 11100110. This is a 1’s complement form of 25.

Let’s take the 2’s complement form of this. So, you have to add 1 to this. 11100111 is a 2’s complement form of 25. 

We finally got this number, which is 11100111. This is 1 byte data. And observe the 7th bit, which is 1 (see Figure 2). So, this is a sign bit that represents data is negative. 

The 7th bit is a sign bit, and the remaining bits are the magnitude. So, magnitude for 25 in 2’s complement form.

 

If I convert -25 into hex form, what happens?

Figure 4. Convert -25 to hex form
Figure 4. Convert -25 to hex form

 

1110 is a lower nibble, and 0111 is an upper nibble. 1110 turns out to be 7, and 1110 turns out to be 14. 14 means E. 11100111 turns out to be 0xE7. 0xE7 is the hex value of decimal -25.

It is very very simple. Remember that, in signed data representation, a dedicated 1 bit is reserved in order to encode the sign, and you are left with only seven bits to represent the magnitude. 

 

Now let’s see another example. Represent the data 25 in 1 byte signed data representation. That is +25.

Figure 5. Example
Figure 5. Example

 

Here the sign is positive. That’s why the most significant bit will be 0. And since the data is positive, you need not convert the magnitude into 2’s complement form. So, you convert that into the binary format here. 00011001 is the binary format of 25. 

If I convert 00011001 into hex, 1001 will be 9, and 0001 will be 1. So, that happens to be 0x19, which is a hex value for decimal +25.

 

Now, based on that, let’s discuss the char data type range. 

Figure 6. Char data type range, when the sign is +ve
Figure 6. Char data type range, when the sign is +ve

 

When the sign is positive, the seventh bit will be zero. So the least value that we can store is all zeroes. That’s why the magnitude will be 0. That is the least possible magnitude. 

And the highest value would be to fill all the magnitude bits with 1. That’s why the highest positive value would be 127.

 

Figure 7. Char data type range, when the sign is -ve
Figure 7. Char data type range, when the sign is -ve

 

When the sign is negative, the seventh bit will be 1. The least value possible is -128. -128 means all magnitude bits are filled with zeros. So, 10000000 is the 2’s complement format for -128. 

And the highest negative value possible is -1. So, when it is -1, all the magnitude bits are filled with 1. 11111111 is the 2’s complement format for -1. 

 

Now we have here two ranges for positive values and negative values. For positive values, it can go from 0 to 127. For negative values, the range would be 0 to -128. So, the range of char data type is -128 to 127. This is a range for char or signed char(Figure 8).  

Figure 8. Char data type range
Figure 8. Char data type range

 

Unsigned char range:

  • The unsigned char data type is used to store 1 byte of unsigned data. 
  • In unsigned data representation no particular bit is dedicatedly used to encode the sign. 
Range calculation for 'char' data type
Figure 9. One byte unsigned data representation

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.