310 likes | 813 Views
Number Systems. Decimal number system Binary number system Hexadecimal number system Converting Negative numbers Character representation. Decimal (base 10). Uses positional representation Each digit corresponds to a power of 10 based on its position in the number
E N D
Number Systems • Decimal number system • Binary number system • Hexadecimal number system • Converting • Negative numbers • Character representation
Decimal (base 10) • Uses positional representation • Each digit corresponds to a power of 10 based on its position in the number • The powers of 10 increment from 0, 1, 2, etc. as you move right to left • 1,479 = 1 * 103 + 4 * 102 + 7 * 101 + 9 * 100
Binary (base 2) • Two digits: 0, 1 • To make the binary numbers more readable, the digits are often put in groups of 4 • 1010 = 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 = 8 + 2 = 10 • 1100 1001 = 1 * 27 + 1 * 26 + 1 * 23 + 1 * 20 = 128 + 64 + 8 + 1 = 201
Hexadecimal (base 16) • Shorter & easier to read than binary • 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F • “0x” often precedes hexadecimal numbers • 0x123 = 1 * 162 + 2 * 161 + 3 * 160 = 1 * 256 + 2 * 16 + 3 * 1 = 256 + 32 + 3= 291
Hexadecimal (base 16) • Another example • 0xABC = A * 162 + B * 161 + C * 160 = 10 * 256 + 11 * 16 + 12 * 1 = 2560 + 176 + 12= 2748
Converting • See “Links” on webpage for link to converter between binary, decimal, and hexadecimal values
Converting • From binary or hex to decimal • Use positional representation as shown previously • From decimal to binary or hex • Keep dividing by 2 (or 16) • Remainders give the digits, starting from lowest power • From binary to hex (or vice versa) • Replace each set of four binary digits by the corresponding hexadecimal digit (or vice versa)
Convert Decimal to Binary • 22 decimal = 10110 binary • Calculations: 2 |22r. 0 2 |11r. 12 | 5r. 12 | 2r. 02 | 1r. 1 0
Convert Decimal to Binary • 22 decimal = 10110 binary • Calculations: • Write down powers of 2 • Subtract the largest power of 2 that is less than the number • Add a 1 if can subtract, and a 0 if cannot 22 6 2 32 16 8 4 2 11 0 1 1 0
Class Exercise • Bin to dec: 1001 0011 • Dec to bin: 105 • Bin to hex: 0010 1110 1000 1011 • Hex to bin: 0xFEDC • Hex to dec: 0x10A • Dec to hex: 165
Other Bases • Base 8 (octal number system) • 123 = 1 * 82 + 2 * 81 + 3 * 160 = 1 * 64 + 2 * 8 + 3 * 1 = 64 + 16 + 3 = 83 • Base 13 • 123 = 1 * 132 + 2 * 131 + 3 * 130 = 1 * 169 + 2 * 13 + 3 * 1 = 169 + 26 + 3= 198
Unsigned Integers • Represents positive integers only • Not necessary to indicate a sign, so all bits can be used for the magnitude: • 1 byte = 8 bits = 28 = 256 (0 to 255) • 255 decimal = 1111 1111 binary • 2 bytes = 16 bits = 216 = 65,536 (0 to 65,535) • 4 bytes = 32 bits = 232 = 4,294,967,296 (0 to 4,294,967,295)
Signed Integers • Represents positive and negative integers • MSB (Most Significant Bit – leftmost bit) used to indicate sign • 0 = positive, 1 = negative • One less bit is used for the magnitude, with one extra negative value • 1 byte = 8-1 bits = 27 (-128 to +127) • -128 decimal = 1000 0000 binary • -1 decimal = 1111 1111 binary • 127 decimal = 0111 1111 binary • 2 bytes = 16-1 bits = 215 (-32,768 to +32,767) • 4 bytes = 32-1 bits = 231 (-2,147,483,648 to +2,147,483,647 )
1’s & 2’s Complement • 1’s complement form • Formed by reversing (complementing) each bit • 2’s complement form • Formed by adding 1 to 1's complement • Negative numbers are stored this way • Additive inverse of a number • Computer never has to subtract • A – B = A + (-B)
Binary to Decimal Conversion • Unsigned Integers • Convert binary directly to decimal form • Signed Integers • If MSB = 0, convert directly to decimal • If MSB = 1, convert to 2's complement form (reverse the bits & add 1), then to decimal form
Signed Integers (8-bit) • Example: -9 0000 1001b = +9 1111 0110b +1b 1111 0111b = -9 • Example: -32 0010 0000b = +32 1101 1111b +1b 1110 0000b = -32 • Convert to decimal 1111 0111b = -9 0000 1000b +1b0000 1001b = +9 • Convert to decimal 1110 0000b = -32 0001 1111b +1b 0010 0000b = +32
Class Exercise • Convert the following negative decimal numbers to 8 bit binary using the 2’s complement • -1, -3, -8, -127
Class Exercise • Evaluate 32 + (- 5) in 8 bit binary • Convert 32 to binary • Convert -5 to 2’s complement • Add together • Check your answer by converting back to decimal
Character Representation • ASCII (See link to ASCII Codes) • American Standard Code for Information Interchange • Standard encoding scheme used to represent characters in binary format on computers • 7-bit encoding, so 128 characters can be represented • 0 to 31 (& 127) are "control characters" (cannot print) • Ctrl-A or ^A is 1, ^B is 2, etc. • Used for screen formatting & data communication • 32 to 126 are printable
Binary Data • Decimal, hex & character representations are easier for humans to understand; however… • All the data in the computer is binary • An int is typically 32 binary digits • int y = 5; (y = 0x00000005;) • In computer y = 00000000 00000000 00000000 00000101 • int z = -5; (y = 0xFFFFFFFB;) • In computer, z = 11111111 11111111 11111111 11111011
Binary Data • A char is typically 8 binary digits • char x = '5'; (or char x = 53, or char x = 0x35) • In computer, x = 00110101 • char x = 5; (or char x = 0x05;) • In computer, x = 00000101 • Note that the ASCII character 5 has a different binary value than the numeral 5 • Also note that 1 ASCII character = 2 hex numbers