Number System-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Computer Programming

CSC01
Audience:
B. Tech. (Semester I, Section: H )
2023-24 (Odd Semester)

Dr. Abhijit Sharma


Assistant Professor
Department of Computer Science & Engineering
NIT Durgapur
CSC01

Binary Arithmetic
Addition
▪ Four basic rules are needed to perform binary addition.
▪ The first three rules are quite simple and there is no difference
between these binary rules and the corresponding decimal rules.
▪ These rules can be used to derive another important rule for binary
arithmetic.
Addition
▪ Three 1’s are added together in binary
Example
▪ Adding 11112(1510) and 01102 (610).
Example
▪ Adding 11112(1510) and 01102 (610).
Subtraction
▪ For subtraction in the decimal system, normally the borrow method is used.
30
-6
▪ Start with the rightmost digit, which is 0. Can we subtract 6 from 0? No, so we need
to borrow from the tens place.
▪ 10 is borrowed from the tens column in order to complete the subtraction in the ones
column.
▪ Moving 10 to the ones column and subtracting 6 yields 4.
▪ The remaining 20 from the tens column is taken and 2 is written in the tens column
to get the result of 24
Subtraction
▪ The borrow method can also be used to do binary subtraction.
▪ The binary subtraction rules are used while performing the binary
subtraction.
▪ The first three rules are similar to the decimal system rules.
▪ The fourth rule, however, needs a little more explanation .since it
defines how borrowing is done from another column.
Subtraction
▪ Consider the problem of subtracting 12 from 102.
Signed and Unsigned Numbers
▪ A binary number may be positive or negative.
▪ Generally, we use the symbol “+” and “−” to represent positive and
negative numbers, respectively.
▪ The sign of a binary number has to be represented using 0 and 1, in the
computer.
Signed and Unsigned Numbers
▪ An n-bit signed binary number consists of two parts—sign bit and
magnitude.
▪ The left most bit, also called the Most Significant Bit (MSB) is the sign bit.
▪ The remaining n−1 bits denote the magnitude of the number.
▪ This approach is called the signed magnitude representation.
Signed and Unsigned Numbers
▪ In signed binary numbers, the sign bit is 0 for a positive number and 1 for a
negative number.
▪ For example, 01100011 is a positive number since its sign bit is 0, and, 11001011
is a negative number since its sign bit is 1.
▪ The range of integers that can be stored in signed magnitude is –2n–1 + 1 to +2n–1 –
1, where n is the number of bits in the signed magnitude number.
▪ An 8-bit signed number can represent data in the range −128 to +127 (−27 to
+27−1). The left-most bit is the sign bit.
▪ In an n-bit unsigned binary number, the magnitude of the number n is stored in n
bits.
▪ An 8-bit unsigned number can represent data in the range 0 to 255 (28 = 256).
Difficulty in Performing Arithmetic with Signed Magnitude

▪ While representing negative numbers with signed magnitude is


simple, performing arithmetic with signed magnitude is difficult.
▪ Ambiguity of Zero: In signed magnitude representation, there are two
representations for zero: positive zero and negative zero.
▪ This can lead to complications in arithmetic operations, as you must ensure that
both representations are treated correctly. For example, adding positive zero and
negative zero should yield zero, not a positive or negative number.
▪ Addition and Subtraction: Addition and subtraction in signed
magnitude representation require additional steps to handle the signs of
numbers.
▪ When adding or subtracting two signed magnitude numbers, you must consider the
signs separately and then adjust the result accordingly. For example, adding two
numbers of opposite signs requires subtraction followed by sign adjustment.
Difficulty in Performing Arithmetic with Signed Magnitude

▪ Carry Propagation: In addition and subtraction operations, carry


propagation (carry-in and carry-out) can be challenging to manage
because of the sign bit.
▪ Carry operations must be handled differently depending on the sign of the
operands, making it more complicated.
▪ Comparison and Ordering: Determining the relative order of two
signed magnitude numbers (greater than, less than, or equal) can be more
complex than in other representations.
▪ You need to consider both magnitude and sign, which requires additional logic.
Difficulty in Performing Arithmetic with Signed Magnitude

▪ Consider the subtraction problem 3010 – 610.


▪ This can be converted to an equivalent addition problem by changing 610
to –610.
▪ Now, the problem may be restated as 3010 + (–610) = 2410.
▪ Something similar can be done in binary by representing negative
numbers as complements.
Complement of Binary Numbers
▪ Complements are used in computer for the simplification of the
subtraction operation.
▪ There are two types of complements for the binary number
system—1’s complement and 2’s complement.
▪ For any number in base r, there exist two complements—(1) r’s
complement and (2) r−1’s complement.
1’s Complement
▪ A signed number with 1’s complement is represented by changing all
the bits that are 1 to 0 and all the bits that are 0 to 1.
▪ Reversing the digits in this way is also called complementing a
number.
▪ 1’s complement of 101 is 010
▪ 1’s complement of 1011 is 0100
▪ 1’s complement of 1101100 is 0010011
▪ –0 and +0 are represented differently even though they are the same
algebraically.
▪ This causes problems when carrying out tests on arithmetic results.
2’s Complement
▪ The 2’s complement of a binary number is obtained by adding 1 to
the 1’s complement representation.
▪ 2’s complement of 101 is 010 + 1 = 011
▪ 2’s complement of 1011 is 0100 + 1 = 0101
▪ 2’s complement of 1101100 is 0010011 + 1 = 0010100
Subtraction using signed 1’s complement representation

▪ In this type of representation, subtraction is carried out by addition of


1’s complement of the negative number.
▪ The sign bit is treated as a number bit in the subtraction method.
▪ For the sign bit being 0, i.e., positive, the magnitude part of the result
is the true value.
▪ For the sign bit being 1, i.e., negative, the magnitude part of the result
is in 1’s complement form.
Subtraction using signed 1’s complement representation

▪ For subtracting a smaller number from a larger number, the 1’s


complement method is implemented as follows:
▪ Determine the 1’s complement of the smaller number.
▪ Add the 1’s complement to the larger number.
▪ Remove the final carry (overflow bit) and add it to the result, i.e., if the sum
exceeds n bits, add the extra bit to the result.
▪ This bit is called the end-around carry
Subtraction using signed 1’s complement representation

▪ Subtract 110 from 710 using 1’s complement.


▪ 110 = 00012 and 710 = 01112

Add the negative value 1110 to 0111. This gives


the sum 10101

Whenever an overflow bit occurs in such a method, add


the bit to the sum to get the correct answer. If there is no
overflow bit, leave the sum as it is.
Subtraction using signed 1’s complement representation

▪ For subtracting a larger number from a smaller number, the 1’s


complement method is as follows:
▪ Determine the 1’s complement of the larger number.
▪ Add the 1’s complement to the smaller number.
▪ There is no carry (overflow). The result has the proper sign bit but the answer
is in 1’s complement of the true magnitude.
▪ Take the 1’s complement of the result to get the final result. The sign of the
result is obtained from the sign bit of the result.
Subtraction using signed 1’s complement representation

▪ Subtracting 710 from 110 using


1’s complement..
▪ 110 = 00012 and 710 = 01112
Subtraction using signed 2’s complement representation

▪ Subtraction for this representation is done by addition of the 2’s


complement of the negative number.
▪ The sign bit is treated as a number bit during subtraction. Thus, the
result is obtained with the sign bit.
▪ When the sign bit is 0, i.e., positive, the magnitude part of the result is
the true value. But when the sign bit is 1, i.e., negative, the magnitude
part of the result is in 2’s complement form.
▪ Therefore, the 2’s complement of the magnitude part of the result gives the
true value. The carry bit that evolves with the sum is ignored in the 2’s
complement method.
Subtraction using signed 2’s complement representation

▪ For subtracting a smaller number from a larger number, the 2’s


complement method is implemented as follows.

▪ Determine the 2’s complement of the smaller number.


▪ Add the 2’s complement to the larger number.
▪ Discard the final carry (there is always one in this case).
Subtraction using signed 2’s complement representation

▪ Subtract 110 from 710 using 2’s complement.


▪ 110 = 00012 and 710 = 01112
Subtraction using signed 2’s complement representation

▪ For subtracting a larger number from a smaller number, the 2’s


complement method is implemented as follows

▪ Determine the 2’s complement of the larger number.


▪ Add 2’s complement to the smaller number.
▪ There is no carry from the leftmost column. The result is in 2’s complement
form and is negative.
▪ Take the 2’s complement of the result to get the final answer.
Subtraction using signed 2’s complement representation

▪ Subtracting 11012(1310) from 10012(910) using 2’s complement

Take the 2’s complement of the sum –01002, i.e., –410.


#include <stdio.h> #include <stdio.h>
int main(){ int main(){
short a = 32767; unsigned short a = 65535;
printf("%d\n", sizeof(a)); printf("%d\n", sizeof(a));
printf("%d\n",a++); printf("%d\n",a++);
printf("%d\n",a++); printf("%d\n",a++);
printf("%d\n",a++); printf("%d\n",a++);
return 0; return 0;
} }
Multiplication
▪ Binary multiplication uses the same techniques as decimal
multiplication. In fact, binary multiplication is much easier because
each multiplying digit is either zero or one.
▪ When performing binary multiplication, remember the following rules.

▪ Copy the multiplicand when the multiplier digit is 1. Otherwise, write a row of
zeros.
▪ Shift the results one column to the left for a new multiplier digit.
▪ Add the results using binary addition to find the product.
Multiplication
▪ Example of multiplying 1102 by 102
Division
▪ Division of binary numbers uses the same technique as division in the
decimal system.
▪ When doing binary division, some important rules need to be
remembered.
▪ When the remainder is greater than or equal to the divisor, write a 1 in the
quotient and subtract.
▪ When the remainder is less than the divisor, write a 0 in the quotient and add
another digit from the dividend.
▪ If all the digits of the dividend have been considered and there is still a
remainder, mark a radix point in the dividend and append a zero.
▪ Remember that some fractions do not have an exact representation in binary, so not all
division problems will terminate.
Division
▪ 112/102 or 310/210
CSC01

Type Conversion in C
Type Conversion in C
▪ Type conversion is a fundamental concept in C programming.
▪ It involves changing the data type of a variable from one type to
another.
▪ Type conversion is essential for performing operations involving
variables of different data types and ensuring compatibility between
them.
▪ In C, there are two types of type conversion:
▪ Implicit Conversion
▪ Explicit Conversion
Type Conversion in Expressions
▪ When a C expression is evaluated, the resulting
value has a particular data type.
▪ If all the variables in the expression are of the
same type, the resulting type is of the same type
as well.
▪ For example, if x and y are both of int type, the
expression x + y is of int type as well.
▪ If the variables of an expression are of different
types, the expression has the same data type as
that of the variable with the largest size data
type present in it.
▪ For example, if x is an int and y is a float, evaluating
the expression x/y causes x to be promoted to float
type before the expression is evaluated.
Conversion by Assignment
▪ Promotions also occur with the assignment operator.
▪ The expression on the right side of an assignment statement is always promoted to
the type of the data object on the left side of the assignment operator.
▪ Note that this might cause a ‘demotion’ rather than a promotion.
▪ If f is a float type and i is an int type, i is promoted to float type in this assignment
statement:
f = i;
▪ In contrast, the assignment statement i = f; causes f to be demoted to type int.
▪ Its fractional part is lost on assignment to i.
▪ Remember that f itself is not changed at all; promotion affects only a copy of the value.
Casting Arithmetic Expressions
▪ Casting an arithmetic expression tells the compiler to represent the value of the
expression in a certain way. In effect, a cast is similar to a promotion, which was
discussed earlier. However, a cast is under the programmer’s control, not the
compiler’s.
▪ For example, if i is a type int, the expression (float)i casts i to float type.
▪ The most common use is to avoid losing the fractional part of the answer in an
integer division.
▪ float sum = 5 / 2; printf("%f", sum);
▪ float sum = (float) 5 / 2; printf("%f", sum);
▪ For example, if i is a type int, the expression (float)i casts i to float type.

You might also like