1 / 21

Intro to CS – Honors I Representing Numbers

Intro to CS – Honors I Representing Numbers. Georgios Portokalidis gportoka@stevens.edu. Today’s Lecture. Numerical systems How do computers represent numbers Operations on integers Signed and unsigned numbers. Numerical Systems. We all know well the decimal system Base 10 system

nitara
Download Presentation

Intro to CS – Honors I Representing Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to CS – Honors IRepresenting 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: 4x102 + 6x101 + 6x100 • 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 dix10i • How about base 2, (N)2? • Can you also do base 16 or 8? Tens Ones Hundreds 4 6 6 102 100 101

  4. Decimal to Binary • A simple algorithm • Let D= the number we wish to convert from decimal to binary • Find P, such that 2P is the largest power of two smaller or equal to D. • Repeat until P<0 • If 2P<=D then • Put 1 into column P • Subtract 2P from D • Else • Put 0 into column P • Subtract 1 from P • (356)10 (??)2

  5. Decimal to Binary (cont’d) • All binary numbers N can be represented as • B = dix2i+ di-1x2i-1+...+d1x21+ d0x20 , • where dibinary digit in position i • Odd numbers have d0=1 and even d0=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 - d0x20)/2  dix2i-1+ di-1x2i-2+...+d1x20 • 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 4 binary digits correspond to one hexadecimal digit • 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

  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 20: 0+1=1.Record the 1. Temporary Result: 1; Carry: 0 • Column 21: 1+1=10. Record the 0, carry the 1.Temporary Result: 01; Carry: 1 • Column 22: 1+0=1 Add 1 from carry: 1+1=10. Record the 0, carry the 1.Temporary Result: 001; Carry: 1 • Column 23: 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 Example with unsigned bytes 1001 1100 +0110 0111 ---------------- 1 0000 0011 The overflowing bit is lost

  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

  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

  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

  15. Bitwise XOR • Takes two numbers and performs the logical XOR (eXclusiveOR) operation on each pair of corresponding bits • Example: • 0100 0110 • XOR 0110 1100 = • 0010 1010

  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  27+12=140  1000 1100, -12  27-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

  18. Overflows and Signed Integers • Integer overflows occur when the result of an operation requires more bits than the length of the number involved 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 The overflowing bit is lost

  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 left 3 = 1100 0000 Example: 1001 1000 shift right 2 = 0010 0110 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 left 3 = 1100 0000 Example: 1001 1000 shift right 2 = 1110 0110 Zeroes are shifted in Shifted-out bits are lost The leftmost bit is used when shifting-in bits from the left

  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

More Related