Microcontroller Embedded C Programming Lecture 27| Integer data type ‘short int’ and value range

  • Post author:
  • Post category:Blog

 

Integer data type ‘short int’ and value range

 

 

In this article, let’s study the integer data types ‘short int’ and value range in C. 

 

Integer data type:  short int and unsigned short int

  • Variable of type short int is used to store 2 bytes of signed data. 
  • Variable of type unsigned short int is used to store 2 bytes of unsigned data. 
  • You can just mention short(for signed) or unsigned short. “int” will be assumed. 
  • Short type variable always consumes 2 bytes of memory irrespective of compilers.

 

Range calculation of short int:

For example, represent the data -25 in 2 byte signed data representation. Here the data is negative.

Integer data type
Figure 1. Example

 

First, take the binary representation, which is a pure binary representation of 25 in 2 byte data representation. That will be this one 0000000000011001.

And after that, take the 1’s complement. 11111111111100110 is a 1’s complement format of 25, where you have to convert all ones into zeros and zeros into ones. 

After that, let’s take 2’s complement of that. For that, you have to add 1 to 1’s complement format; you get 2’s complement format. 1111111111100111 is the 2’s complement format of  -25. 

Integer data type
Figure 2. 2’s complement format representation of -25 using 2 bytes

 

Here, the 15th bit, that is the last bit, the most significant bit will be 1 to indicate the data is negative, and the rest of the bits will be used to store the 2’s complement format of the magnitude, that is 25. 

 

The hex value of -25 will be 0xFFE7. So, that is the 2 byte signed data representation of -25 (Figure 3). 

Integer data type
Figure 3. Convert -25 to hex form in 2 byte signed data representation

 

Now represent the data +25 in 2 bytes signed data representation in hex form. So, that will be 0x0019 in hex.

 Convert +25 to hex form in 2 byte signed data representation
Figure 4. Convert +25 to hex form in 2 byte signed data representation

 

 

Range calculation of short int

  • Short range: -32,768 to 32,767
  • Unsigned short range: 0 to 65535. 

Because for unsigned data representation in 2 bytes, you have all the 16 bits to represent the magnitude. So, there is no sign bit. That’s why all 16 bits will be used to store the magnitude. So, the least value will be 0, and the highest value will be  65535, which is nothing but 0xFFFF in hex.  

Two byte unsigned data representation
Figure 5.Two byte unsigned data representation

 

In the following article, let’s understand int and unsigned int. 

 

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.

Leave a Reply