 Open in App
Not now

# Sign Extend a Nine-Bit Number in C

• Difficulty Level : Hard
• Last Updated : 27 Nov, 2022

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

• 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.

Example 1:

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″

Example 2:

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

Approach:

1. First, append leading zeroes to the 9-bit number to make it a 16-bit number using AND operator.
2. Check if left most bit(sign-bit of number) is 0 or 1.
3. if the leftmost bit is 1 then, append leading 1’s to the number by using OR operator.
4. We can see the sign-extended number is having the same value as before but its size is increased because of appended extra bits.

Example:

## C

 `// C program for Sign Extension ` `// of a 9-bit number ` `#include ` ` `  `// Function for Sign Extending ` `// of a number to 16-bit ` `void` `signExtend(``short` `number) ` `{ ` `    ``// Printing value and size of the ` `    ``// number before sign extending ` `    ``printf``(``"Before Sign extension:\n"``); ` `    ``printf``(``"number=%d, size=%d\n\n"``, number, ` `           ``sizeof``(number)); ` ` `  `    ``// Appending leading zeroes to ` `    ``// the 9-bit number ` `    ``int` `ans = number & 0b0000000111111111; ` ` `  `    ``// Checking if sign-bit of number is 0 or 1 ` `    ``if` `(number & 0b100000000) { ` ` `  `        ``// If number is negative, append ` `        ``// leading 1's to the 9-bit sequence ` `        ``ans = ans | 0b0000000111111111; ` `    ``} ` ` `  `    ``// Printing value and size of ` `    ``// the number after sign extending ` `    ``printf``(``"After Sign extension to 16 bits:\n"``); ` `    ``printf``(``"number=%d, size=%d\n"``, ans, ``sizeof``(ans)); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``short` `n = 142; ` `    ``signExtend(n); ` `    ``return` `0; ` `}`

Output

```Before Sign extension:
number=142, size=2

After Sign extension to 16 bits:
number=142, size=4```

My Personal Notes arrow_drop_up
Related Articles