You are on page 1of 14

Simple Data Types in C

Alan L. Cox
alc@rice.edu
Everything is Just a Bunch of Bits

Bits can represent many different things


 Depends on interpretation

You and your program must keep track of


what kind of data is at each location in the
computer’s memory

Cox Simple Data Types 2


Data types in C
Integers

To get the exact size of


a type or a variable on a
particular platform, you
can use
the sizeof operator.

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

Dr. Fenghui Yao Tennessee State University


Binary Systems Department of Computer Science, Nashville, TN 4
Signed Magnitude
◆ The leftmost bit is the sign bit (0 is + and 1
is - ) and the remaining bits hold the
absolute magnitude of the number
◆ Examples
✓ -47 = 1 0 1 0 1 1 1 1
✓ 47 = 0 0 1 0 1 1 1 1

For 8 bits, we can represent the signed integers –128 to +127


How about for N bits?

Dr. Fenghui Yao Tennessee State University


Binary Systems Department of Computer Science, Nashville, TN 5
One’s complement
◆ Replace each 1 by 0 and each 0 by 1
◆ Example (-6)
➢ First represent 6 in binary format (00000110)
➢ Then replace (11111001)

Dr. Fenghui Yao Tennessee State University


Binary Systems Department of Computer Science, Nashville, TN 6
Two’s complement
◆ Find one’s complement
◆ Add 1
◆ Example (-6)
➢ First represent 6 in binary format (00000110)
➢ One’s complement (11111001)
➢ Two’s complement (11111010)

Dr. Fenghui Yao Tennessee State University


Binary Systems Department of Computer Science, Nashville, TN 7
Definitions:
4-Bit Example

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

Formula for calculating Signed & 1’s: -(2n-1 – 1) , (2n-1 – 1)


the range → 2’s: -2n-1 , (2n-1 – 1)
9
CK Cheng, UC San Diego
Overflow
unsigned int x = 2123456789; z is 951,946,282 not
unsigned int y = 3123456789; 5,246,913,578 as
unsigned int z; expected

z = x + y;

int x = 2123456789; y is not a valid positive


int y = 3123456789; number (sign bit is set)!
int z; It’s -1,171,510,507

z = x + y; z is still 951,946,282

Cox Simple Data Types 10


short x = 32234 ;
short y = 10000;
short z=x+y;
if (z>x)
printf("z greater");
if (x+y>x)
printf("x+y greater");

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!

One of many common mistakes:

?
unsigned int u;
… ? What’s wrong?
if (u > -1) …

Always false(!) because -1 is converted to unsigned


expressions involving signed and unsigned types have
all operands promoted to unsigned types.
Cox Simple Data Types 14

You might also like