Number System-2
Number System-2
Number System-2
CSC01
Audience:
B. Tech. (Semester I, Section: H )
2023-24 (Odd Semester)
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
▪ 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.