Académique Documents
Professionnel Documents
Culture Documents
Bit-level manipulations
Boolean algebra Expressing in C
class02.ppt
15-213 F02
Thats why fingers are known as digits Natural representation for financial transactions
Floating point number cannot exactly represent $1.20
Implementing Electronically
Hard to store
ENIAC (First electronic computer) used 10 vacuum tubes / digit
Hard to transmit
Need high precision to encode 10 signal levels on single wire
15-213, F02
Binary Representations
Base 2 Number Representation
Represent 1521310 as 111011011011012 Represent 1.2010 as 1.0011001100110011[0011]2 Represent 1.5213 X 104 as 1.11011011011012 X 213 Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires
0 1 0
Electronic Implementation
3.3V
2.8V
0.5V 0.0V
3 15-213, F02
Conceptually very large array of bytes Actually implemented with hierarchy of different memory types
SRAM, DRAM, disk Only allocate for regions actually used by program
Where different program objects should be stored Multiple mechanisms: static, stack, and heap In any case, all allocation within single virtual address space
15-213, F02
to to to
Or 0xfa1d37b
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
15-213, F02
Machine Words
Machine Has Word Size
15-213, F02
Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015
15-213, F02
Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)
Addr = 0008 ??
Addr = 0008 ??
Addr = 0012 ?? 7
Data Representations
Sizes of C Objects (in Bytes)
Typical 32-bit
4 4 1 2 4 8 8 4
Intel IA32
4 4 1 2 4 8 10/12 4
4 8 1 2 4 8 8 8
15-213, F02
Byte Ordering
How should bytes within multi-byte word be ordered in memory?
Conventions
15-213, F02
Little Endian
Example
Big Endian
01
Little Endian
23
45
67
67
10
45
23
01
15-213, F02
Text representation of binary machine code Generated by program that reads the machine code
Example Fragment
Address 8048365: 8048366: 804836c: Instruction Code 5b 81 c3 ab 12 00 00 83 bb 28 00 00 00 00 Assembly Rendition pop %ebx add $0x12ab,%ebx cmpl $0x0,0x28(%ebx)
Deciphering Numbers
11
0x12ab 0x000012ab 00 00 12 ab ab 12 00 00
15-213, F02
12
15-213, F02
Result (Linux):
int a = 15213; 0x11ffffcb8 0x6d
0x11ffffcb9
0x11ffffcba 0x11ffffcbb
13
0x3b
0x00 0x00
15-213, F02
Representing Integers
int A = 15213; int B = -15213; long int C = 15213;
Linux/Alpha A 6D 3B 00 00 Linux/Alpha B Sun A 00 00 3B 6D Sun B Decimal: 15213 Binary: Hex: Linux C 6D 3B 00 00 0011 1011 0110 1101 3 B 6 D Sun C 00 00 3B 6D
Alpha C 6D 3B 00 00 00 00 00 00
93 C4 FF FF
14
FF FF C4 93
15-213, F02
Representing Pointers
int B = -15213; int *P = &B;
Alpha Address Hex: Binary: Sun P EF FF FB 2C 1 F F F F F C A 0 0001 1111 1111 1111 1111 1111 1100 1010 0000
Alpha P A0 FC FF FF 01 00 00 00
Sun Address Hex: Binary: E F F F F B 2 C 1110 1111 1111 1111 1111 1011 0010 1100 Linux P D4 F8 FF BF
Linux Address Hex: Binary: B F F F F 8 D 4 1011 1111 1111 1111 1111 1000 1101 0100
Representing Floats
Float F = 15213.0;
Linux/Alpha F 00 B4 6D 46 Sun F 46 6D B4 00
IEEE Single Precision Floating Point Representation Hex: Binary: 15213: 4 6 6 D B 4 0 0 0100 0110 0110 1101 1011 0100 0000 0000 1110 1101 1011 01
Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious
16 15-213, F02
Representing Strings
Strings in C
Compatibility
17
Reduced Instruction Set Computer (RISC) PCs use variable length instructions Complex Instruction Set Computer (CISC)
Representing Instructions
int sum(int x, int y) { return x+y; }
Alpha sum 00 00 30 42 01 80 FA 6B
Sun sum 81 C3 E0 08 90 02 00 09
PC sum 55 89 E5 8B 45 0C 03 45 08 89 EC 5D C3
For this example, Alpha & Sun use two 4-byte instructions
Use differing numbers of
compatible
Boolean Algebra
Developed by George Boole in 19th Century
And
Or
A&B = 1 when both A=1 and B=1 & 0 1
0 0 0 1 0 1
Not
~A = 1 when A=0
0 0 1 1 1 1 Exclusive-Or (Xor)
~ 0 1 1 0
20
^ 0 1 0 0 1 1 1 0
15-213, F02
A&~B A ~A ~B
B ~A&B
= A^B
21
15-213, F02
Integer Algebra
Integer Arithmetic
Z, +, *, , 0, 1 forms a ring Addition is sum operation Multiplication is product operation is additive inverse 0 is identity for sum 1 is identity for product
22
15-213, F02
Boolean Algebra
Boolean Algebra
{0,1}, |, &, ~, 0, 1 forms a Boolean algebra Or is sum operation And is product operation ~ is complement operation (not additive inverse) 0 is identity for sum 1 is identity for product
23
15-213, F02
Commutativity A|B = B|A A&B = B&A Associativity (A | B) | C = A | (B | C) (A & B) & C = A & (B & C) Product distributes over sum A & (B | C) = (A & B) | (A & C) Sum and product identities A|0 = A A&1 = A Zero is product annihilator A&0 = 0 Cancellation of negation ~ (~ A) = A
A * (B + C) = A * B + B * C
A+0 = A A*1 =A A*0 = 0 ( A) = A
15-213, F02
24
Boolean: Sum distributes over product A | (B & C) = (A | B) & (A | C) A + (B * C) (A + B) * (B + C) Boolean: Idempotency A|A = A A +AA
A is true or A is true = A is true
A *AA A + (A * B) A
A * (A + B) A
A + A 1
25
Boolean Ring
Property
Boolean Ring
A^B = B^A A&B = B&A (A ^ B) ^ C = A ^ (B ^ C) (A & B) & C = A & (B & C) A & (B ^ C) = (A & B) ^ (B & C) A^0 = A A&1 = A A&0=0 A^A = 0
15-213, F02
26
Commutative sum Commutative product Associative sum Associative product Prod. over sum 0 is sum identity 1 is prod. identity 0 is product annihilator Additive inverse
A and B are true if and only if neither A nor B is false A | B = ~(~A & ~B) A or B are true if and only if A and B are not both false
Exactly one of A and B is true A ^ B = (A | B) & ~(A & B) Either A is true, or B is true, but not both
27
15-213, F02
28
15-213, F02
Width w bit vector represents subsets of {0, , w1} aj = 1 if j A 01101001 { 0, 3, 5, 6 } 76543210 01010101 76543210
{ 0, 2, 4, 6 }
Operations
& | ^ ~
{ 0, 6 } { 0, 2, 3, 4, 5, 6 } { 2, 3, 4, 5 } { 1, 3, 5, 7 }
29
15-213, F02
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C
0xBE
--> 101111102
~0x00 -->
~000000002
0xFF
--> 111111112
--> -->
0x41 0x7D
15-213, F02
30
&&, ||, !
View 0 as False
Anything nonzero as True Always return 0 or 1 Early termination
Shift Operations
Left Shift:
x << y
Right Shift:
x >> y
Logical shift
Fill with 0s on left
Arithmetic shift
Replicate most significant bit on
32
15-213, F02
Bitwise Xor is form of addition With extra property that every value is its own additive inverse
A^A=0 *x Begin 1 2 3 End A A^B A^B
*y B B (A^B)^B = A A A
15-213, F02
(A^B)^A = B B
33
Main Points
Its All About Bits & Bytes
Basic form encodes false as 0, true as 1 General form like bit-level operations in C
Good for representing & manipulating sets
34
15-213, F02