42 Boolean Logic:42布尔逻辑
IST/ICT/CSc/4.1 Name:
4.1 Number Systems
Calculation
In binary, ,
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1, or 102
, Note that these are the same rules as in decimal. , In decimal,
7 + 5 = 2 carry 1, or 12
, When doing addition, you always “carry” the overflow left i.e. add 1 into the
column on the left.
, Remember hexadecimal uses 0 1 2 3 4 5 6 7 8 9 A B C D E F before overflowing
, In hexadecimal,
7 + 5 = C (no need to carry)
9 + A = 3 carry 1, or 1316
C + E = A carry 1, or 1A16
, Subtraction involves “borrowing” from the left column. If nothing is available, the
answer is negative.
, In decimal,
13 – 9 = -6 with a borrow of 10 = -6 + 10 = 4 , In binary,
0 - 0 = 0
1 - 1 = 0
1 - 0 = 1
10 - 1 = -1 borrow 10, or 1
, Of course if the last example were 0 – 1, the answer would be -1. , In hexadecimal,
9 - 3 = 6
D - 5 = 8
14 - 8 = -4 borrow 10, or C 16
10 - B = 5
Floating Point Numbers
, Real numbers have a fractional part e.g. 3.142 , They can also be less than one or negative e.g. -0.00385 4 , 1966.0058 can be represented as 0.19660058 x 10(called scientific notation) , When there is nothing before the point, the number is normalised. ,3 -225.61 = -0.22561 x 10,-3 -0.000256 = -0.256 x 104, In 0.19660058 x 10, 0.19660058 is called the mantissa, 4 is called the
exponent.
, Move the decimal or binary point so there is 0 on its left (this becomes the
mantissa)
, The number of places moved becomes the exponent , N.B. works equally for decimal and (two’s complement) binary
, Normalisation converts fixed point real numbers into floating point.
- 1 - Dec 30, 2014
IST/ICT/CSc/4.1 Name:
Integer versus Floating Point Format
, Integers
have a smaller range, so overflow errors can occur
at least all the values inside their range can be stored accurately.
overflow errors can be caught since the range of a data type is known (4 bytes
for an int, 8 for a long).
, Real numbers
the range of numbers that can be represented is much greater.
allow fractional values
not all numbers within the range can be represented precisely (leading to
possible truncation errors)
More About Errors
, Truncation errors can occur when a real number is assigned to an integer
variable
, Assigning 3.142 to an int will store 3 – the 0.142 is lost.
, Overflow errors can occur when the number to be stored is larger than the
available number of bits e.g. trying to put the number 1000 into a byte variable.
, Floating point overflow errors occur if numbers with either the mantissa or
exponent are too large to fit into the allocated bits.
, Overflow errors can be caught and handled in Java.
, Underflow errors can occur when an attempt is made to store a number that is
too small to be represented - not reported as errors in Java and since they
default to zero can cause run-time errors e.g. divide by zero
- 2 - Dec 30, 2014