Goals for this Week - csc.villanova.edumdamian/Past/csc2400fa12/notes/03_BitsAndBytes.pdf15 Decimal...
Transcript of Goals for this Week - csc.villanova.edumdamian/Past/csc2400fa12/notes/03_BitsAndBytes.pdf15 Decimal...
1
CSC 2400: Computer Systems
Bits, Bytes and Data Types
2
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
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
1 0 1 0 1
V(t)
5
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
6
Coding
• A single binary input can have two values: 1 or 0
• More bits = more combinations
• How many values can you represent on 3 bits?
• What about n bits?
0 0 0 1 1 0 1 1
Memorize Powers of 2
Powers of 2 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 28 = 256 29 = 512
Powers of 10 210 = 1Kilo (1024) 103
= 1,000
220 = 1Mega (10242 = 1,048,576 ) 106 = 1,000,000 230 = 1Giga (10243 = 1,073,741,824) 109
= 1,000,000,000 240 = 1Tera (10244 = 1,099,511,627,776) 1012
= 1,000,000,000,000
7
8
Base 10 and Base 2 • Decimal (base 10)
• Each digit represents a power of 10 • 417310 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100
• Binary (base 2) • Each bit represents a power of 2 • 10102 = 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 1010
9
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
100000112 = _________________________________10 ? 1010011002 = _________________________________10 ?
Place Value
10
Decimal to Binary
• Use the Placement Method
1024 512 256 128 64 32 16 8 4 2 1
Powers of 2
15510 = _________________________________ 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
11
You Try It …
Working w/ Bits is Tedious for People
• Express large binary numbers in hex using ¼ fewer digits:
• This is the hexadecimal system. Memorize the tables above. 12
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
1. Based on the lookup tables on the previous page, convert the hex value 0x7A8BF7D6 into its binary equivalent:
2. Based on the lookup tables on the previous page, convert the binary 10 0110 1110 1001 0100 1100 0101 11112 to hex:
Exercises
0111 1010 1000 1011 1111 0111 1101 0110
0010 0110 1110 1001 0100 1100 0101 1111
6 E 9 4 B 5 F 2
7 A 8 B F 7 D 6
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?
15
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
2
1
6
1
4
0
0
8 5 + E D
• 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:
Hex Addition
A
C 3 + 2 7
C 7 + 2 A
E 1
1
F 2
1
1 7
3 B 9 A + D 2 E 6
1
8
1
E 1 0
D 3 5 F - 4 3 B 6 /
• 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:
Hex Subtraction
D
B 3 - 3 6
C A - 2 5
7 5 A
/
9 A
1 A
/ 1 1
2 C
F 8
18
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
1 9 8
+ 2 6 4
Sum
Carry
0 1 1
+ 0 0 1
Sum
Carry
2
1
6
1
4
0
Base 10 Base 2
20
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 2n
• Examples of unsigned integers • 00000001 1 • 00001111 ____ • 00010000 ____ • 00100001 ____ • 11111111 ____
21
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 2n doesn’t change the answer • For eight-bit number, n=8 and 2n=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
22
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!)
• Two’s complement • Add 1 to the one’s complement • E.g., (255 – 69) + 1 ____________
- 0100 0101 1111 1111
b
one’s complement
23
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
24
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
25
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
26
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
27
Question
• What value does 10011001 represent? [Ex. 6, page 69]
28
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
29
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
Bitwise Operations
30
31
Networking
32
Encryption/ Decryption
33
Compression
34
Bitwise Operators: AND and OR
• Bitwise AND (&)
• Bitwise OR (|)
&
0
1
0 1 |
0
1
0 1
0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1
___
& 15
___
0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1
___
| 15
___
35
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
36
Bitwise Operators: Shift Left/Right
• Shift left (<<): Multiply by powers of 2 • Shift some # of bits to the left, filling the blanks with 0
• Shift right (>>): Divide by powers of 2 • Shift some # of bits to the right. Fill in blanks with sign bit.
0 0 1 1 0 1 0 1 53
53<<2
0 0 1 1 0 1 0 1 53
53>>2
1 0 1 1 0 1 0 1 -75
-75>>2
sign extension sign extension
Practice: Ch. 2, Exercises 10, 12, 13, 14
37
• Used to change or query one or more bits in a variable.
• The bitmask indicates which bits are to be affected.
• Common operations:
Can extend to groups of bits
Bitmasks
Operation Expression C expression
Set Nth bit of x x = x OR 2N x = x |(1 << N);
Clear Nth bit of x x = x ________ x = _____________;
Read Nth bit of x ??? = x ______ ??? = x _________;
38
Exercise
• Let x be an 8-bit integer.
• Set the 5th least significant bit of x:
• Clear the 5th least significant bit of x:
39
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"); }
40
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
42
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
44
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
45
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; }
46
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 */ }
47
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
48
Required Reading
• Textbook, Chapter 2