Sign Extend a Nine-Bit Number in C
Prerequisite: Bitwise operators in C
In this article, we will see what is Sign extension and how to significantly extend a nine-bit number with a C program. This method can be used for signing to extend a nine-bit number to 32-bit, extend nine-bit to 64-bit, extend 16-bit to 32-bit, etc. It follows the sign extension rules so can be used in every case.
- Sign extension of a number is performed by widening the binary number without changing the sign and value of the number.
- This process is done by using signed bit representation, where left most bit represents the sign of the number (0 if positive and 1 if negative).
- To this number, additional digits(0 for positive and 1 for negative integers) are inserted to the most significant side of the signed bit number such that the bit count is increased but the sign and value of the number are preserved.
Consider number 130, and its 9-bit value is “010000010“.
since the underlined left most bit is 0, it is positive, add zeroes to the left side of the bit sequence.
RESULT : 130 extended to 16-bits : “00000000 10000010″
Consider number -210, and its 9-bit value is “11010010″.
since the left most bit is 1, it is negative so add leading ‘1’s to the left of underlined bit.
RESULT : -210 extended to 16-bits : “11111111 1010010″
Sign Extending 9-bit number in C
- First, append leading zeroes to the 9-bit number to make it a 16-bit number using AND operator.
- Check if left most bit(sign-bit of number) is 0 or 1.
- if the leftmost bit is 1 then, append leading 1’s to the number by using OR operator.
- We can see the sign-extended number is having the same value as before but its size is increased because of appended extra bits.
Before Sign extension: number=142, size=2 After Sign extension to 16 bits: number=142, size=4
Please Login to comment...