Download presentation
Presentation is loading. Please wait.
Published byRahul Deason Modified over 9 years ago
1
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU
2
Today’s Lecture Numerical systems How do computers represent numbers Operations on integers Signed and unsigned numbers
3
Numerical Systems We all know well the decimal system Base 10 system The number can be actually expressed as: 4x10 2 + 6x10 1 + 6x10 0 So a number N base 10 is denoted as (N) 10 ◦uses digits 0..9 ◦And a digit d in position i has the value d i x10 i How about base 2, (N) 2 ? Can you also do base 16 or 8? 4 6 6 Ones Tens Hundreds 10 2 10 1 10 0 2929 2828 2727 2626 2525 2424 23232 2121 2020 5122561286432168421
4
Decimal to Binary A simple algorithm Let D= the number we wish to convert from decimal to binary Find P, such that 2 P is the largest power of two smaller or equal to D. Repeat until P<0 ◦If 2 P <=D then ◦Put 1 into column P ◦Subtract 2 P from D ◦Else ◦Put 0 into column P ◦Subtract 1 from P (356) 10 (??) 2 Position9876543210 2P2P 2929 2828 2727 2626 2525 2424 23232 2121 2020 Value5122561286432168421
5
Decimal to Binary (cont’d) All binary numbers N can be represented as B = d i x2 i + d i-1 x2 i-1 +...+d 1 x2 1 + d 0 x2 0, where d i binary digit in position i Odd numbers have d 0 =1 and even d 0 =0 ◦This reveals the rightmost bit of N We need to shift the number to the right by 1 bit to again calculate its last bit ◦(B - d 0 x2 0 )/2 d i x2 i-1 + d i-1 x2 i-2 +...+d 1 x2 0 An alternative algorithm Let D= the number we wish to convert from decimal to binary Repeat until D equals 0 ◦Divide D with 2 (D/2) ◦Prepend the remainder of the division to the left of the binary number ◦Let D be the quotient of the division (257) 10 (??) 2
6
Hexadecimal Base 16 number system Digits are [0..9] [A..F] ◦Letters are case insensitive Converting from binary to hexadecimal ◦There are some benefits 16 being a power of 2 ◦What is 1010 0010 in hex? ◦Answer: 0xC2 ◦Note the ‘0x’ prefix! Converting from decimal to hex ◦Use the same algorithm as for converting decimal to binary ◦(76) 10 (??) 16 HexadecimalBinaryDecimal 000000 100011 200102 300113 401004 501015 601106 701117 810008 910019 A101010 B101111 C101012 D110113 E111014 F111115 4 binary digits correspond to one hexadecimal digit
7
Number Representation Computer systems use the binary numerical system Numbers need to be stored in fixed-size elements, such as the registers, hence they cannot be arbitrarily long ◦The smallest addressable piece of memory is a byte - 8 bits ◦CPU registers are 32-bit or 64-bit long Most Significant Bit (MSB) = Leftmost bit in number, the bit with the highest value Least Significant Bit (LSB) = Rightmost bit in number, the bit with the smallest value
8
Binary Addition 1010 +1111 -------- Cheat sheet: 0+0=0 1+0=1 1+1=10 Column 2 0 : 0+1=1. Record the 1. Temporary Result: 1; Carry: 0 Column 2 1 : 1+1=10. Record the 0, carry the 1. Temporary Result: 01; Carry: 1 Column 2 2 : 1+0=1 Add 1 from carry: 1+1=10. Record the 0, carry the 1. Temporary Result: 001; Carry: 1 Column 2 3 : 1+1=10. Add 1 from carry: 10+1=11. Record the 11. Final result: 11001
9
Binary Multiplication 1010 x 11 --------- Cheat sheet 0x0=0 1x0=0 0x1=0 1x1=1 Multiplying by 2 is easy, just shift in (append) a 0 from the right How about multiplying with 4 or 8?
10
Overflows Integer overflows occur when the result of an operation requires more bits than the length of the number involved The overflowing bit is lost Example with unsigned bytes 1001 1100 +0110 0111 ---------------- 1 0000 0011
11
Binary Division 111011 / 11 Same rules as in decimal division When dividing integers the remainder is ignored
12
Bitwise NOT or One’s Complement Bitwise operations are logical operations on the individual bits of one or two numbers Performs logical negation of each bit in the number ◦Just flip each bit’s value Example: NOT 0100 0110 = 1011 1001 operandNOT operand 01 10
13
Bitwise AND Takes two numbers and performs the logical AND operation on each pair of corresponding bits Example: 0100 0110 AND 0110 1100 = 0100 0100 operandoperand’ AND result 000 010 100 111
14
Bitwise OR Takes two numbers and performs the logical OR operation on each pair of corresponding bits Example: 0100 0110 OR 0110 1100 = 0110 1110 operandoperand’ OR result 000 011 101 111
15
Bitwise XOR Takes two numbers and performs the logical XOR (eXclusive OR) operation on each pair of corresponding bits Example: 0100 0110 XOR 0110 1100 = 0010 1010 operandoperand’ OR result 000 011 101 110
16
Signed Numbers Sign-magnitude notation ◦Use the leftmost bit of a number as the equivalent of a sign: “0” is “+”, “1” is “-” ◦Example: 12 0000 1100, -12 1000 1100 One’s complement ◦Flip all bits. The leftmost bit still indicates signed-ness ◦Example: 12 0000 1100, -12 1111 0011 Two’s complement ◦Flip all bits, and add 1. The leftmost bit still indicates signed-ness ◦Example: 12 0000 1100, -12 1111 0100 Excess 2 (m-1) ◦m is the length of the number in bits. Every number is represented by adding it to 2 (m-1) ◦Example: 12 2 7 +12=140 1000 1100, -12 2 7 -12=116 0111 0100 ◦Numbers are the same as two’s complement with sign bit flipped The largest signed number is smaller, than the largest unsigned number
17
Why So Many Ways? Let’s perform the following binary additions (-5+12), (-12+-5), and (12+-12) Two’s complement makes the addition of both signed and unsigned integers Different programming languages may use different ways of representing numbers Java uses two’s complement for signed numbers Sign-magnitude notation One’s complementTwo’s complementExcess 2 (m-1) 120000 1100 1000 1100 -121000 11001111 00111111 01000111 0100 -51000 01011111 10101111 10110111 1011
18
Overflows and Signed Integers Integer overflows occur when the result of an operation requires more bits than the length of the number involved The overflowing bit is lost Example with unsigned bytes 1001 1100 +0110 0111 ---------------- 1 0000 0011 Example with signed bytes 0001 1100 +0110 0111 ---------------- 1000 0011 The overflow happens into the sign bit
19
Shifting Bits The bits of a number can be moved or shifted to the left or right Numbers are stored in fixed-size registers in the CPU ◦Moving bits can cause bits to shift-out and others to shift-in Logical shift ◦Integers are treated as bit-strings ◦Appropriate for unsigned integers Example: 1001 1000 shift right 2 = 0010 0110 Example: 1001 1000 shift left 3 = 1100 0000 Zeroes are shifted in Shifted-out bits are lost
20
Shifting Bits The bits of a number can be moved or shifted to the left or right Numbers are stored in fixed-size registers in the CPU ◦Moving bits can cause bits to shift-out and others to shift-in Logical shift ◦Integers are treated as bit-strings ◦Appropriate for unsigned integers Arithmetic shift ◦Integers are treated as numbers ◦Appropriate for signed integers Example: 1001 1000 shift right 2 = 1110 0110 Example: 1001 1000 shift left 3 = 1100 0000 The leftmost bit is used when shifting-in bits from the left Zeroes are shifted in Shifted-out bits are lost
21
Summary Numbers can be represented in different numerical systems Computer use the binary system Arithmetic operations like addition, multiplication, and division are straightforward Bitwise operations perform actions on individuals bits of numbers There are multiple ways to represent signed numbers ◦Two’s complement is the one used in most computers due to its simplicity Numbers in the CPU have fixed length, so beware of overflows Shift operations allow you to move bits in a number ◦The bits shifted-in from the left depend on whether we are performing an arithmetic or logical shift
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.