Bits Bytes and Integers: CSCI 232: Computer Organization Week 2
Bits Bytes and Integers: CSCI 232: Computer Organization Week 2
1.1V
0.9V
0.2V
0.0V
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Encoding Byte Values
0 0 0000
Byte = 8 bits 1 1 0001
▪ Binary 000000002 to 111111112 2 2 0010
3 3 0011
▪ Decimal: 010 to 25510 4 4 0100
▪ Hexadecimal 0016 to FF16 5 5 0101
6 6 0110
▪ Base 16 number representation 7 7 0111
8 8 1000
▪ Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 9 9 1001
▪ Write FA1D37B16 in C as A 10 1010
B 11 1011
– 0xFA1D37B C 12 1100
– 0xfa1d37b D 13 1101
E 14 1110
F 15 1111
Decimal to binary by successive divisions
char 1 byte 1 1
short 2 bytes 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8
▪ 01101001 { 0, 3, 5, 6 }
▪ 76543210
▪ 01010101 { 0, 2, 4, 6 }
▪ 76543210
Operations
▪ & Intersection 01000001 { 0, 6 }
▪ | Union 01111101 { 0, 2, 3, 4, 5, 6 }
▪ ^ Symmetric difference 00111100 { 2, 3, 4, 5 }
▪ ~ Complement 10101010 { 1, 3, 5, 7 }
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C
0 0 0000
▪ Apply to any “integral” data type 1 1 0001
▪ long, int, short, char, unsigned 2 2 0010
3 3 0011
▪ View arguments as bit vectors 4 4 0100
5 5 0101
▪ Arguments applied bit-wise 6 6 0110
Examples (Char data type) 7 7 0111
8 8 1000
▪ ~0x41 → 0xBE 9 9 1001
~0100 000122→
~01000001
▪ →10111110
1011 11102 2 A 10 1010
B 11 1011
▪ ~0x00 → 0xFF C 12 1100
▪ ~0000
~00000000
000022→→11111111
1111 11112 2 D 13 1101
E 14 1110
▪ 0x69 & 0x55 → 0x41 F 15 1111
▪ 0110
01101001
100122&&01010101
0101 01012 2→→01000001
0100 0001
2 2
Undefined Behavior
▪ Shift amount < 0 or ≥ word size
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18
Logic Shift Operation
#include<stdio.h>
int main()
Output
{
// a = 5(00000101), b = 9(00001001) a<<1 = 10 b<<1 = 18
unsigned char a = 5, b = 9;
-16 8 4 2 1
10 = 0 1 0 1 0 8+2 = 10
-16 8 4 2 1
-10 = 1 0 1 1 0 -16+4+2 = -10
Values for W = 16
Decimal Hex Binary
UMax 65535 FF FF 11111111 11111111
TMax 32767 7F FF 01111111 11111111
TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000
Observations C Programming
▪ |TMin | = TMax + 1 ▪ #include <limits.h>
▪ Asymmetric range ▪ Declares constants, e.g.,
▪ UMax = 2 * TMax + 1 ▪ ULONG_MAX
▪ LONG_MAX
▪ LONG_MIN
▪ Values platform specific
w–1 0
ux + + + ••• +++
x - ++ ••• +++
Casting
▪ Explicit casting between signed & unsigned same as U2T and T2U
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
k copies of MSB w
X •••
•••
X ••• •••
k
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
w 38
Sign Extension: Simple Example
-16 8 4 2 1 -16 8 4 2 1
10 = 0 1 0 1 0 -10 = 1 0 1 1 0
-32 16 8 4 2 1 -32 16 8 4 2 1
10 = 0 0 1 0 1 0 -10 = 1 1 0 1 1 0
#include <stdio.h>
#include <math.h>
int main() truncated value of
{ 17.070000 is 17.000000
double x=17.07,y=46.107,z=234.867; truncated value of
printf("truncated value of %f is %f\n",x,trunc(x)); 46.107000 is 46.000000
printf("truncated value of %f is %f\n",y,trunc(y)); truncated value of
printf("truncated value of %f is %f\n",y,trunc(z)); 234.867000 is 234.000000
getch();
return 0;}
Truncation
Task:
▪ Given k+w-bit signed or unsigned integer X
▪ Convert it to w-bit integer X’ with same value for “small enough” X
Rule:
▪ Drop top k bits:
▪ X = xw–1 , xw–2 ,…, x0
k w
X ••• •••
•••
X •••
w 41
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Modulus Method
2 mod 16 example
The modulus method requires us to first find out what the highest common
multiple of the Divisor (16) is that is equal to or less than the Dividend (2).
We can see that multiples of 16 are 0, 16, 32, 48, etc. The highest multiple
that is less than or equal to 2 is 0.
So the final step in the modulus method here is to subtract the divisor
highest multiple from the Dividend and answer the question "what is 2
modulus 16?":
2-0=2
Truncation: Simple Example
No sign change Sign change
-16 8 4 2 1 -16 8 4 2 1
2 = 0 0 0 1 0 10 = 0 1 0 1 0
-8 4 2 1 -8 4 2 1
2 = 0 0 1 0 -6 = 1 0 1 0
2 mod 16 = 2 10 mod 16 = 10U mod 16 = 10U = -6
-16 8 4 2 1 -16 8 4 2 1
-6 = 1 1 0 1 0 -10 = 1 0 1 1 0
-8 4 2 1 -8 4 2 1
-6 = 1 0 1 0 6 = 0 1 1 0
-6 mod 16 = 26U mod 16 = 10U = -6 -10 mod 16 = 22U mod 16 = 6U = 6
• Each type can specify a size with keyword char, short, long, as well as
an indication of whether the represented numbers are all
nonnegative (declared as unsigned), or possibly negative (the
default.)
Typical ranges for C integral data types for
32-bit programs
Typical ranges for C integral data types for
64-bit programs
Guaranteed ranges for C integral data types
AURAK
•••
53
AURAK
Machine Words
Any given computer has a “Word Size”
Nominal size of integer-valued data
and of addresses
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
long double − − 10/16
pointer 4 8 8
56
AURAK
Byte Ordering
So, how are the bytes within a multi-byte word ordered in
memory?
Conventions
Big Endian: Sun, PPC Mac, Internet
Least significant byte has highest address
Little Endian: x86, ARM processors running Android, iOS, and
Windows
Least significant byte has lowest address
57
AURAK
58
AURAK
Decimal: 15213
Representing Integers Binary: 0011 1011 0110 1101
Hex: 3 B 6 D
59
AURAK
Representing Pointers
int B = -15213;
int *P = &B;
declare an integer
pointer ‘p’ and assign Sun IA32 x86-64
the address of ‘b’ to it EF AC 3C
FF 28 1B
FB F5 FE
2C FF 82
FD
7F
00
00
Representing Strings
char S[6] = "18213";
Strings in C
Represented by array of characters
Each character encoded in ASCII format IA32 Sun
Standard 7-bit encoding of character set 31 31
Character “0” has code 0x30 38 38
– Digit i has code 0x30+i 32 32
String should be null-terminated 31 31
Final character = 0
33 33
Compatibility 00 00
Byte ordering not an issue
63