Page 53 - Cs_withBlue_J_C11_Flipbook
P. 53

Toggling 0’s and 1’s, we get 1’s complement of 00100011 is

                     1’s complement representation   1        1       0        1       1        1       0       0
                 Adding 1 to 1’s complement, we get 2’s complement.

                     2’s complement representation   1        1       0        1       1        1       0       1

                 Example 2: Write the 8-bit representation of decimal number -49 using the following notations.
                 (a) Signed Magnitude (b) One’s Complement (c) Two’s complement

                 Answer: Binary equivalent of 49 is:       2          49

                                                           2          24           1
                                                           2          12           0
                                                           2           6           0
                                                           2           3           0

                                                           2           1           1
                 (49)  = 110001 2                                      0           1
                    10
                 In 8-bit representation, we have to add a leading 0 in magnitude bit and 1 in sign bit as the number is negative.

                     Signed bit representation        1       0        1       1        0       0        0       1

                 Toggling 0’s and 1’s in 00110001, we get 1’s complement.

                     1’s complement representation    1       1        0       0        1       1        1       0
                 Adding 1 to 1’s complement, we get 2’s complement.

                     2’s complement representation    1       1        0       0        1       1        1       1

                 2.1.4 Signed and Unsigned Shift Operators
                 Shift operators are used to move the bits of an integer to the left or right. The bit pattern is given by the left-hand
                 operand and the shift value by the right-hand operand.

                 There are three types of shift operators which are as follows:
                 •  Signed left shift operator “<<” shifts a bit pattern towards MSB (left) by a specified number of bits and fills the empty
                   LSB bits with 0.
                 •  Signed right shift operator “>>” shifts a bit pattern to LSB (right) by a given number of bits and the vacant right MSB
                   bits are filled with the sign bit.
                 •  Unsigned right shift operator “>>>” unlike right shift operator, the vacant leftmost positions created after shifting,
                   are filled with 0 instead of sign bit.
                 Let us see how these operators work. The operations are discussed assuming 8-bit integers.

                 Example 1: 28 << 1
                 Binary equivalent of 28 is 11100.
                 Representing it as 8 bits and adding 0 in sign bit for positive number, we get:

                                       0       0        0       1        1       1        0       0
                 Shifting each bit by 1 position to the left and filling zero to the right bit, the bit pattern change s as shown below.

                                       0       0        1       1        1       0        0       0


                                                                                                                        51
                                                                                                            Encoding    51
   48   49   50   51   52   53   54   55   56   57   58