CSC 2400: Computer Systems Bits, Bytes and Data Types 1 Goals for this Week Binary number system Why binary? Converting between decimal and binary and octal and hexadecimal number systems Finite representations of binary integers Unsigned and signed integers Integer addition and subtraction Bitwise operators AND, OR, NOT, and XOR Shift-left and shift-right 2
Analog/Analogue Systems Analogue Systems V(t) can have any value between its minimum and maximum value V(t) Digital Systems Digital Systems V(t) takes a value selected from a set of values Binary digital systems form the basis of almost all hardware systems currently V(t) 1 0 1 0 1
Why Bits (Binary Digits)? Computers are built using digital circuits Inputs and outputs can have only two values True (high voltage) or false (low voltage) Represented as 1 and 0 Can represent many kinds of information Boolean (true or false) Numbers (23, 79, ) Characters ( a, z, ) Pixels, sounds Internet addresses Can manipulate in many ways Read and write Logical operations Arithmetic 5 Coding A single binary input can have two values: 1 or 0 More bits = more combinations 0 0 0 1 1 0 1 1 How many values can you represent on 3 bits? What about n bits? 6
Memorize Powers of 2 Powers of 2 2 3 = 8 2 4 = 16 2 5 = 32 2 6 = 64 2 7 = 128 2 8 = 256 2 9 = 512 Powers of 10 2 10 = 1Kilo (1024) 10 3 = 1,000 2 20 = 1Mega (1024 2 = 1,048,576 ) 10 6 = 1,000,000 2 30 = 1Giga (1024 3 = 1,073,741,824) 10 9 = 1,000,000,000 2 40 = 1Tera (1024 4 = 1,099,511,627,776) 10 12 = 1,000,000,000,000 7 Base 10 and Base 2 Decimal (base 10) Each digit represents a power of 10 4173 10 = 4 x 10 3 + 1 x 10 2 + 7 x 10 1 + 3 x 10 0 Binary (base 2) Each bit represents a power of 2 1010 2 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 0 x 2 0 = 10 10 8
Binary to Decimal Sum up the place values of all 1-bits: 256 128 64 32 16 8 4 2 1 1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 0 Place Value 10000011 2 = 10? 101001100 2 = 10? 9 Decimal to Binary Use the Placement Method Powers of 2 1024 512 256 128 64 32 16 8 4 2 1 155 10 = 2? 128 goes into 155 once leaving 27 to be placed 1??????? 64 and 32 are too big (make them 0) 16 goes in once leaving 11 1 0 0 1???? and so on 10
You Try It 11 Working w/ Bits is Tedious for People Express large binary numbers in hex using ¼ fewer digits: Hex Binary Decimal 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 Hex Binary Decimal 8 1000 8 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 This is the hexadecimal system. Memorize the tables above. 12
Exercises 1. Based on the lookup tables on the previous page, convert the hex value 0x7A8BF7D6 into its binary equivalent: 7 A 8 B F 7 D 6 0111 1010 1000 1011 1111 0111 1101 0110 2. Based on the lookup tables on the previous page, convert the binary 10 0110 1110 1001 0100 1100 0101 1111 2 to hex: 0010 0110 1110 1001 0100 1100 0101 1111 2 6 E 9 4 B 5 F Why hexadecimal? Widely used in assembly language programming Used in network programming and debugging Used in graphic design programs e.g., for the red, green and blue components of a color: FF0000 represents red, for example. How many bits are used to represent each color? How many different colors can be represented?
Decimal Addition From right to left, we add each pair of digits We write the sum, and add the carry to the next column 1 9 8 + 2 6 4 Sum Carry 4 6 2 0 1 1 15 Hex Addition Hex addition is similar to decimal addition except that each hex digit has a range of 0 to F instead of 0 to 9, and a carry out occurs when the sum of hex digits in a particular column exceeds F (15 decimal). Examples: 1 C 3 C 7 8 5 + 2 7 + 2 A + E D E A F 1 1 7 2 1 1 1 3 B 9 A + D 2 E 6 1 0 E 8 0
Hex Subtraction Hex subtraction is similar to decimal subtraction except that if the subtrahend is greater than the minuend, we must borrow 16 (10 hex) from the previous digit. Examples: C A - 2 5 A 5 A 1 / B 3-3 6 7 D C 1 2 / / 1 D 3 5 F - 4 3 B 6 8 F A 9 Recall: Decimal Addition From right to left, we add each pair of digits We write the sum, and add the carry to the next column Base 10 Base 2 1 9 8 + 2 6 4 Sum Carry 4 6 2 0 1 1 0 1 1 + 0 0 1 Sum Carry 18
Finite Representation of Integers Fixed number of bits in memory Usually 8, 16, or 32 bits (1, 2, or 4 bytes) Unsigned integer No sign bit Always 0 or a positive number All arithmetic is modulo 2 n Examples of unsigned integers 00000001 1 00001111 00010000 00100001 11111111 20 Modulo Arithmetic Consider only numbers in a range E.g., five-digit car odometer: 0, 1,, 99999 E.g., eight-bit numbers 0, 1,, 255 Roll-over when you run out of space E.g., car odometer goes from 99999 to 0, 1, E.g., eight-bit number goes from 255 to 0, 1, Adding 2 n doesn t change the answer For eight-bit number, n=8 and 2 n =256 E.g., (37 + 256) mod 256 is This can help us do subtraction Suppose you want to compute a b Note that this equals a + (256-1 - b) + 1 21
One s and Two s Complement One s complement: flip every bit E.g., b is 01000101 (i.e., 69 in decimal) One s complement is That s simply 255-69 Subtracting from 11111111 is easy (no carry needed!) 1111 1111-0100 0101 b one s complement Two s complement Add 1 to the one s complement E.g., (255 69) + 1 22 Putting it All Together Computing a b Same as a + 256 b Same as a + (255 b) + 1 Same as a + onescomplement(b) + 1 Same as a + twoscomplement(b) Example: 172 69 The original number 69: One s complement of 69: Two s complement of 69: Add to the number 172: The sum comes to: Equals: 103 in decimal 23
Reading Two s Complement Patterns When a two s complement number has a highest bit 1, it indicates that the number is negative. To find the value, perform the same steps: Unknown value: 1011 1011 One s complement: Add 1 (two s complement): We get a value of 69, so the original pattern must have been -69. Practice: Ch. 2, Exercise 2 24 Signed Integers Sign-magnitude representation Use one bit to store the sign Zero for positive number One for negative number Examples E.g., 0010 1100 44 1010 1100-44 Hard to do arithmetic this way, so it is rarely used Complement representation One s complement Flip every bit E.g., 1101 0011-44 Two s complement Flip every bit, then add 1 E.g., 1101 0100-44 25
Fill in the Table Bit Pattern Value (Sign Magnitude) Value (One s Complement) Value (Two s Complement) 000 001 010 011 100 101 110 111 26 Question What value does 10011001 represent? [Ex. 6, page 69] 27
Overflow: Running Out of Room Adding two large integers together Sum might be too large to store in the number of bits available What happens? Unsigned integers All arithmetic is modulo arithmetic Sum would just wrap around Signed integers Can get nonsense values Example with 16-bit integers Sum: 10000+20000+30000 Result: -5536 28 Exercise Assume only four bits are available for representing integers, and signed integers are represented in 2 s complement. Compute the value of the expression 7 + 7 29
Bitwise Operations 30 Networking 31
Encryption/ Decryption 32 Compression 33
Bitwise Operators: AND and OR Bitwise AND (&) Bitwise OR ( ) & 0 0 1 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 & 15 0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 34 Bitwise Operators: Not and XOR One s complement (~) Turns 0 to 1, and 1 to 0 E.g., set last three bits to 0 x = x & XOR (^) 0 if both bits are the same 1 if the two bits are different ^ 0 1 0 1 Practice: Ch. 2, Exercise 9 35
Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2 Shift some # of bits to the left, filling the blanks with 0 53 0 0 1 1 0 1 0 1 53<<2 Shift right (>>): Divide by powers of 2 Shift some # of bits to the right. Fill in blanks with sign bit. 53 0 0 1 1 0 1 0 1-75 1 0 1 1 0 1 0 1 53>>2-75>>2 sign extension sign extension Practice: Ch. 2, Exercises 10, 12, 13, 14 36 Bitmasks Used to change or query one or more bits in a variable. The bitmask indicates which bits are to be affected. Common operations: Operation Expression C expression Set N th bit of x x = x OR 2 N x = x (1 << N); Clear N th bit of x x = x x = ; Read N th bit of x??? = x??? = x ; Can extend to groups of bits 37
Exercise Let x be an 8-bit integer. Set the 5 th least significant bit of x: Clear the 5 th least significant bit of x: 38 Bitmask Example /* This program demonstrates setting a bit, clearing a bit, and ** reading a bit. (pg 64) */ #include <stdio.h> main() { char a; int i; a=17; a=a (1 << 3); /* set 3rd bit */ printf("%d\n",a); a=a & (~(1<<4)); /* clear 4th bit */ printf("%d\n",a); } for (i=7; i>=0; i--) printf("%d ",(a&(1<<i)) >> i); /* read i'th bit */ printf("\n"); 39
Exercise #1 Write a small program that reads in an integer and prints out its binary representation. Sample output: Please enter an integer: 1025 The binary representation of 1025 is 00000000 00000000 00000100 00000001 40 Exercise #2 Write a small program that reads in an integer prints out hexadecimal value of its bytes, separated by spaces, starting with the most significant byte. Sample output: Please enter an integer: 1025 The four bytes (in hexadecimal) are: 00 00 04 01 42
Example: Counting the 1 s How many 1 bits in a number? E.g., how many 1 bits in the binary representation of 53? Four 1 bits How to count them? 0 0 1 1 0 1 0 1 44 Counting the Number of 1 Bits #include <stdio.h> #include <stdlib.h> int main(void) { unsigned n, count; printf( Number: "); if (scanf("%u", &n)!= 1) { fprintf(stderr, "Error: Expect number.\n"); exit(exit_failure); } /* Enter code to count the 1 bits */ } printf( Number of 1 bits: %u\n, count); return 0; 45
Practice Write a function getbyte that extracts a byte from a word. /* * getbyte(x, n) - Extract byte n from word x * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples: getbyte(0x12345678,1) = 0x56 * Legal operations: ~ & ^ + << >> */ int getbyte(int x, int n) { /* Add code here */ } 46 Summary Computer represents everything in binary Integers, floating-point numbers, characters, addresses, Pixels, sounds, colors, etc. Binary arithmetic through logic operations Sum (XOR) and Carry (AND) Two s complement for subtraction Binary operations in C AND, OR, NOT, XOR, shift left and shift right Useful for efficient and concise code, though sometimes cryptic 47
Required Reading Textbook, Chapter 2 48