180 likes | 195 Views
Learn about integer data types, overflow, signed and unsigned integers, magnitude of numbers, and arithmetic operations in binary.
E N D
Integers Whole numbers Do NOT contain decimal points (as in money) 43,689 is an integer 43,689.24 is NOT an integer (it is floating point) Overflow A number of bits are allocated to hold a number: such as a byte (8 bits) or a word (4 bytes) The numeric data (number) being placed into the address is larger than the allocated # of bits can hold The program will abend (abnormal termination – blow off) Ex. Using 8 bits (standard byte) Will hold the numbers 0 through 255 Try to put the number 306 into the byte and an overflow occurs
Unsigned Integers Whole numbers (integer) Do NOT contain decimal points (as in money) 43,689 is an integer 43,689.24 is NOT an integer (it is floating point) Sample Ranges: 4 bits 0 through 15 8 bits 0 through 255 16 bits 0 through 65,535 Ex. Using 8 bits: Decimal 165 has a bit pattern of 10100101 Decimal 37 has a bit pattern of 00100101 Note: the highest bit can be either a 1 or a 0. The number is still a positive (+) number Add the weight values to determine the decimal value Unsigned integers are positive by default
Signed Integers A number can be positive or negative The high order bit determines if + or – 1 0 1 0 0 1 0 1 is a negative number 0 0 1 0 0 1 0 1 is a positive number Magnitude (size) of a Number The magnitude of a number can only be determined if the number is positive A negative number has already been converted (by One’s Compliment) and is not representative of the true value A negative number must be converted to positive number to determine the true magnitude of the number (do One’s Complement then Two’s Complement) The bits below the sign bit determine the magnitude of the number (the 1st 7 bits of an 8 bit byte – the 8th bit is the sign)
A computer does not do: Subtraction Multiplication Division All arithmetic is done with ADDITION Addition is accomplished via the use of two methods: One’s Compliment Method Two’s Compliment Method
Convert Binary to Decimal Signed Integers A number can be positive or negative The high order bit determines if + or – 1 0 1 0 0 1 0 1 is a negative number 0 0 1 0 0 1 0 1 is a positive number If a number is positive (+) just add up the weights of the bits If a number is negative (-) = left (upper) most bit is a 1 The left (upper) most bit will be a 1 therefore the number is negative To determine the true value of the #: Do the Ones’ then Two’s Complement Given: 0 0 0 0 1 1 1 0 Weight values total to 14 High bit is a 0 therefore the number is positive The number is +14 (this is the magnitude) 1 0 0 0 1 1 1 0 High bit is a 1 therefore the number is negative The true value is unknown (this is not the magnitude of the number – the magnitude is determined by the number being positive – the number will have to be One’s Complemented the Two’s Complemented)
One’s Complement The way data is stored in the computer To complement a number: Change any 0 to a 1 Change any 1 to a 0 0 0 1 0 0 1 0 12 is a positive +3710(add the weight values) stored in the computer. The number is true. Do not need to complement a positive (high order bit a 0) number to determine the magnitude. One’s Complement of the +3710 = 1 1 0 1 1 0 1 02 is the One’s Complement of +3710 High order bit is a “one” so the number is negative This does not represent the true – value (yet) A negative number is a complimented version of the positive number with the high order bit set to a 1 Only a positive number will give the true magnitude of the number.
Two’s Complement Second part of the process to convert binary numbers to/from positive and negative A computer cannot subtract, multiply, or divide A computer does all arithmetic with some form of addition Two’s Complement process Add a binary 1 to the binary byte Given: 01110010 positive binary byte (high order bit 0) + 1 Two’s Complement -------------- 01110011 Two’s Complement result 1 Given: 10001101 negative binary byte (high order bit 1) + 1 Two’s Complement -------------- 10001110 Two’s Complement result This results in the Two’s Compliment number
Conversion Rule To convert to or from a + or – binary number do: One’s Complement first Then Two’s Complement
Convert Binary to Decimal Signed Integers • Positive to negative number: • Given binary positive byte 00000010 = +2 • One’s Complement the byte: 11111101 = unknown # • 1 • 2) Two’s Complement the byte: 11111101 • + 1 • ------------- • 11111110 = -2 Representation • Negative to positive number: • Given binary negative byte 11111110 = -2 • One’s Complement the byte: 00000001 = unknown # • 1 • 2) Two’s Complement the byte: 00000001 • + 1 • -------------- • 00000010 = +2
Convert Binary to Decimal Signed Integers Negative to Positive Binary Conversion Given the negative binary #: 1 0 0 0 1 1 1 0 High bit is a 1 therefore the number is negative The true value is unknown This is not the magnitude of the number – the magnitude is determined by the number being positive – the number will have to be One’s Complemented then Two’s Complemented The (-) Binary One’s Complement Two’s Number Complement Complement 1 10001110 01110001 01110001 + 1 -------------- 01110010 = 11410
Two’s Complement ADDITION Unsigned: 5 +3 --- 8 1 1 1 5 = 0 1 0 12 3 = 0 0 1 12 --------- 8 = 1 0 0 02 No One’s or Two’s Complement required Signed: high order bit = sign (+ or -) 5 +3 --- 8 1 1 1 5 = 0 1 0 12 +3 = 0 0 1 12 the only valid results are 0 - 7 --------- The addition will “blow off” with overflow High order is for the “sign” The sign going to a 1 would make it negative Only the low order bits are for the magnitude
Two’s Complement Subtraction Is accomplished through Addition Unsigned Subtraction 1) 510 (+) -310 ---- 210 2) 5 = 01012 3 = 11002 One’s Complement on the negative 3 1 3) 5 = 0 1 0 1 3 = 1 1 0 0 ---------- 1 0 0 0 1 Add the two numbers 4) 5 = 0 1 0 1 3 = 1 1 0 0 ---------- 0 0 0 1 1 Add binary one (Two’s Complement) --------- 0 0 1 0 = 210
Two’s Complement Subtraction Is accomplished through Addition Signed Subtraction 1) 510 (+) -310 0011 = 1100 complement = a -3 ---- find the value of a -3 bit pattern 210 2) 5 = 01012 3 = 11002Rule 1: do One’s Complement 1 3) 5 = 0 1 0 1 3 = 1 1 0 0 ---------- 1 0 0 0 1 Rule 2: Add 4) 5 = 0 1 0 1 3 = 1 1 0 0 ---------- 0 0 0 1 1 Rule 3: Two’s Complement --------- 0 0 1 0 = 210 Magnitude (Value) of the positive result is 2 970cs111_objects1.ppt
Two’s Complement Subtraction • Is accomplished through Addition • Signed Subtraction • 1) 310 0011 • (+) -510 0101 • ---- • -210 ? • -5 = 1010 Rule 1: do One’s Complement • 3) 3 = 0 0 1 1 • -5 = 1 0 1 0 • ---------- • 1 1 0 1 Rule 2: Add • 1 • 1 1 0 1 Rule 3: Two’s Complement • 1 Add binary one • --------- • 1 1 1 0 = -210 bit pattern for a +2
Two’s Complement Subtraction Confirmation • Is accomplished through Addition • Signed Subtraction • Find out the real value of this negative number • ? 1110 (a negative 2?) • 0001 Rule 1: do One’s Complement • 1 • 3) 0 0 0 1 Rule 2 for converting a negative # to a • + 1 positive # - do Two’s Complement • ---------- • 0 0 1 0 Binary magnitude of 2
Two’s Complement Subtraction: another sample • Is accomplished through Addition • Signed Subtraction • 1) 6210 0 0 1 1 1 1 1 0 • (+) -7510 0 1 0 0 1 0 1 1 • ---- • -210 ? • -75 = 1 0 1 1 0 1 0 0 Rule 1: do One’s Complement • 1 1 1 1 • 3) 62 = 0 0 1 1 1 1 1 0 • -75 = 1 0 1 1 0 1 0 0 • --------------------- • 1 1 1 1 0 0 1 0 Rule 2: Add • 1 1 1 1 0 0 1 0 Rule 3: Two’s Complement • 1 Add binary one • ------------------- • 1 1 1 1 0 0 1 1 = -1310 bit pattern for a +13
Two’s Complement Subtraction Confirmation • Is accomplished through Addition • Signed Subtraction • Find out the real value of this negative number • ? 1 1 1 1 0 0 1 1 • 0 0 0 0 1 1 0 0 Rule 1: do One’s Complement • 0 0 0 0 1 1 0 0 Rule 2 for converting a negative # • + 1 to a positive # - do Two’s • ------------------- Complement • 0 0 0 0 1 1 0 1 Binary magnitude of 13
Conversions Convert a POSITIVE number to a NEGATIVE number +2 0 0 1 0 1 1 1 0 1 Take One’s Complement of +2 1 Do Two’s Complement ----------- 1 1 1 0 = -2 version of +2 Convert a NEGATIVE number to a POSITIVE number -2 1 1 1 0 1 0 0 0 1 Take One’s Complement of -2 1 Do Two’s Complement ----------- 0 0 1 0 = +2 version of -2