Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position 3 2 1 0 Digits 2 1 7 A Value = 2x16 3 + 1x16 2 + 7x16 + Ax1 = 2x4096 + 1x256 + 7x16.

Similar presentations


Presentation on theme: "Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position 3 2 1 0 Digits 2 1 7 A Value = 2x16 3 + 1x16 2 + 7x16 + Ax1 = 2x4096 + 1x256 + 7x16."— Presentation transcript:

1 Unsigned and Signed Numbers

2

3 Hexadecimal Number 217A 16 Position 3 2 1 0 Digits 2 1 7 A Value = 2x16 3 + 1x16 2 + 7x16 + Ax1 = 2x4096 + 1x256 + 7x16 + Ax1 = 10000 Converting from Hexadecimals

4 Signed and Unsigned Integers unsigned int a, b, c; // represented as 32 bit binary numbers c = a + b ; // OK c = a- b ; // potentially error if b > a A Possible Solution: Declare a signed integer valuable int d; d = a – b; // still could be an error. Why ? What will be the value of d if a = 0x80000001; b = 0x00000001; d = a – b; // d= - 0x80000000 ??

5 int counter = 0x7FFFFFFF; Question: What will be the new value of the counter variable after being incremented? Answer: - 0x80000000 (yes, it will be negative) 2’s Complement Integer Representation

6 Expressing Negative Numbers Procedure: Step 1: Invert Bitwise Step 2: Add One Example: Express -0x00000003 in 2’s complement ~ 0x00000003 = 0xFFFFFFFC (Step 1) 0xFFFFFFFC+1 = 0xFFFFFFFD (Step 2) Note: Rules of Arithmetics remain unchanged: 0xFFFFFFFD + 0x00000003 = 0x00000000

7 Using unsigned integers unsigned int count = 0x00001234; while (count ≥ 0){ -- count ; } Will the above loop terminate ?? int count = 0x00001234; while (count ≥ 0){ ++ count ; } What will be the exit value of the count variable ??

8 int main(){ int counter = 0; while ( counter < 0x7) { ++ counter; if ( (counter & 1) != 0 ){ /* Do something when counter value is even */ /* Insert your code here..... */ } return 0; } expression counter & 1 evaluates as TRUE only if the value of counter is odd We are bitwise “anding” the variable with a mask constant. Using Mask to select Individual Bits

9 Test: Use the bitwise OR operator | to set the bit #5 in the variable counter. Answer: counter = counter | 0x00000020; Use the bitwise AND operator & to clear the bit #5 in the variable counter. Answer: counter = counter & 0xFFFFFFDF; Setting and Clearing Bits using OR and AND operators


Download ppt "Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position 3 2 1 0 Digits 2 1 7 A Value = 2x16 3 + 1x16 2 + 7x16 + Ax1 = 2x4096 + 1x256 + 7x16."

Similar presentations


Ads by Google