210 likes | 348 Views
Bits and Bytes. COMP 21000 Comp Org & Assembly Lang. Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations Numbers Characters and strings Instructions Bit-level manipulations Boolean algebra Expressing in C. Why Don’t Computers Use Base 10?.
E N D
Bits and Bytes COMP 21000 Comp Org & Assembly Lang Topics • Why bits? • Representing information as bits • Binary / Hexadecimal • Byte representations • Numbers • Characters and strings • Instructions • Bit-level manipulations • Boolean algebra • Expressing in C
Why Don’t Computers Use Base 10? Base 10 Number Representation • That’s why fingers are known as “digits” • Natural representation for financial transactions • Floating point number cannot exactly represent $1.20 • Even carries through in scientific notation • 15.213 X 103 (1.5213e4)
Why Don’t Computers Use Base 10? Implementing Electronically • Hard to store • ENIAC (First electronic computer) used 10 vacuum tubes / digit • IBM 650 used 5+2 bits (1958, successor to IBM’s Personal Automatic Computer, PAC from 1956) • Hard to transmit • Need high precision to encode 10 signal levels on single wire • Messy to implement digital logic functions • Addition, multiplication, etc.
0 1 0 3.3V 2.8V 0.5V 0.0V 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 Electronic Implementation • Easy to store with bistable elements • Reliably transmitted on noisy and inaccurate wires
Decimal Binary Hex 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Encoding Byte Values Byte = 8 bits • Binary 000000002 to 111111112 • Decimal: 010 to 25510 • First digit must not be 0 in C • Octal: 0008 to 03778 • Use leading 0 in C • Hexadecimal 0016 to FF16 • Base 16 number representation • Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ • Use leading 0x in C, • write FA1D37B16 as 0xFA1D37B • Or 0xfa1d37b
Bases Every number system uses a base Decimal numbers use base 10 Other common bases used in computer science: • Octal (base 8) • Hexadecimal (base 16) • Binary (base 2)
Decimal 753 7 x 102 + 5 x 101 + 3 x 100
Octal 753 7 x 82 + 5 x 81 + 3 x 80
Converting Converting octal to decimal: • 753 = 7 x 82 + 5 x 81 + 3 x 80 = 448 + 40 + 3 = 491
Translating to octalwriting 157 in base 8 157 remainder: 8 19 5 2 3 0 2 2 3 5 2 x 82 + 3 x 81 + 5 x 80 = 128 + 24 + 5 = 157 Divisor
Hexadecimal 753 7 x 162 + 5 x 161 + 3 x 160
Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 10A 10B 10C 10D 10E 10F 120 …
Translating to hex 365 remainder: 16 22 D 1 6 0 1 1 6 D 1 x 162 + 6 x 161 + D x 160 = 256 + 96 + 13 = 365
Binary 101 1 x 22 + 0 x 21 + 1 x 20
Translating to binary 4 remainder: 2 2 0 1 0 0 1 Divisor 1 0 0
Translating to binary 11 remainder: 2 5 1 2 1 1 0 0 1 1 0 1 1
Literary Hex Common 8-byte hex filler: • 0xdeadbeef • Can you think of other 8-byte fillers?
Relation between hex and binary • Hexadecimal: • 16 characters, • Each represents a number between 0 and 15 • Binary: • Consider 4 places • Represent numbers between 0000 and 1111 • Or between 0 and 15 • Result: • 1 hex digit represents 4 binary digits