Problem Solving and Computer Programming: Assignment in Programming Languages
Problem Solving and Computer Programming: Assignment in Programming Languages
PROGRAMMING
05:
Lecture 05:
Assignment in
Programming Languages
NITW -- PSCP 05 1
2’s complement notation for negative
numbers
• In order to find the 2’s complement of a negative
integer , -N
– take 1’s complement of binary representation of N
– Add binary 1 to it.
• Binary representation:
• 52: 00000000 00000000 0000000000110100
• 1’s Complement:
• -52: 11111111111111111111111111001011
• 2’s Complement
• -52: 11111111111111111111111111001100
NITW -- PSCP 05 15
Representation of integers
• Integers are converted in to binary notation
using 2’s complement representation.
• Advantage: Each of the 2ⁿ bit patterns
represents a distinct integer.
• In case n = 4, the numbers are from -8 to +7,
which is -2³ to 2³ -1.
• In general the range of integers that can
stored on a 32 bit machine is:
• -2³¹ to 2³¹ -1.
NITW -- PSCP 05 17
Fractional Number Representation
• Details of representation of floating point numbers
and IEEE standard will be covered in courses on
Computer Architecture.
• For basic understanding, floating point numbers are
stored in sign-magnitude form
Sign Exponent Fraction / mantissa
Bit 0 Bits 1-8 Bits 9 - 31
• Sign is 0 for positive, 1 for negative. Next 8 bits
are exponent and fraction is 23 bits.
• The number is sign * mantissa * 2^exponent
NITW -- PSCP 05 18
floating point number representation
• Single precision numbers include sign bit, an 8-bit
exponent field and a 23-bit fraction, for a total of 32
bits.
• Double precision numbers include sign bit, an 11-bit
exponent field and a 52-bit fraction, for a total of 64
bits.
• In the normalized representation of floating point
numbers, mantissa is taken as 1 + f. For example, if f
is 01101…, the mantissa would be 1.01101…
• The e field represents the exponent as a biased
number – It contains the actual exponent plus 127
for single precision.
NITW -- PSCP 05 19
Conversions
• If e is 93, then the actual exponent is 93-127 = -34. If
e is 129, then the actual exponent is 2.
• The decimal equivalent of
1 01111100 11000000000000000000000
sign is -1, e = 124, f = 0.75. Hence the floating point
number is (-1)*(1.75)*2^-3 = -0.21875
• On a 32 bit machine integer 4 is represented as
• 00000000 00000000 00000000 00000100
• Floating point number 4.0 is represented in
normalized form as
• 0 10000001 0000000 00000000 00000000
NITW -- PSCP 05 20
Decimal Floating point number to binary
representation in normalized form
• What is the single-precision representation of
347.625?
• 347.625 = 101011011.101₂.
• 101011011.101 x 2⁰ = 1.01011011101 x 2⁸
• f = fraction = 0101101 11010000 00000000
• e = exponent + 127 = 135 = 10000111
• s = Sign = 0
• The representation is
• 0100001110101101 11010000 00000000
NITW -- PSCP 05 21