Professional Documents
Culture Documents
Simple Data Types in C: Alan L. Cox Alc@rice - Edu
Simple Data Types in C: Alan L. Cox Alc@rice - Edu
Alan L. Cox
alc@rice.edu
Everything is Just a Bunch of Bits
3
https://www.tutorialspoint.com/cprogramming/c_data_types.htm
Negative Binary Numbers
◆ Three different systems have been
used
➢ Signed magnitude
➢ One’s complement
➢ Two’s complement
NOTE: For negative numbers the sign bit is always 1, and for positive
numbers it is 0 in these three systems
8
CK Cheng, UC San Diego
Definitions: Examples
Given n-bits, what is the range of my numbers in each
system?
• 3 bits: • 5 bits:
– Signed: -3 , 3 – Signed: -15, 15
– 1’s: -3 , 3 – 1’s: -15, 15
– 2’s: -4 , 3 – 2’s: -16, 15
• 6 bits • Given 8 bits
– Signed: -31, 31 – Signed: -127, 127
– 1’s: -31, 31 – 1’s: -127, 127
– 2’s: -32, 31 – 2’s: -128, 127
z = x + y;
z = x + y; z is still 951,946,282
11
short x = 32234 ;
short y = 10000;
short z=x+y;
if (z>x)
printf("z greater");
if (x+y>x)
printf("x+y greater");
12
short x = 32234 ;
short y = 10000;
short z=x+y;
if (z>x)
printf(" z>x ");
if (x+y>x)
printf("x+y>x");
x+y>x
13
Beware of Sign Conversions in C
Beware implicit or explicit conversions between
unsigned and signed representations!
?
unsigned int u;
… ? What’s wrong?
if (u > -1) …