Binary Data Answers

  1. basic, unsigned binary representation:
    1. Which of these unsigned binary numbers are even and which are odd?
      1. 111111110111101000101010110110110
        even
        
      2. 100101001010101001110111111011101
        odd
        
      3. 100101010010101010010101010101010
        even
        
      4. 101111111111111111111111111111111
        odd
        
    2. Convert the following unsigned binary numbers into decimal
      1. 1111111
        64 + 32 + 16 + 8 + 4 + 2 + 1 = 127
        
      2. 11111111
        128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
        
      3. 101010
        32 + 8 + 2 = 42
        
      4. 011011
        16 + 8 + 2 + 1 = 27
        
    3. For all the unsigned binary numbers above, convert them into hexadecimal
      0001 1111 1110 1111 0100 0101 0101 1011 0110
      1    F    E    F    4    5    5    B    6
      
      0001 0010 1001 0101 0100 1110 1111 1101 1101
      1    2    9    5    4    E    F    D    D
      
      0001 0010 1010 0101 0101 0010 1010 1010 1010
      1    2    A    5    5    2    A    A    A
      
      0001 0111 1111 1111 1111 1111 1111 1111 1111
      1    7    F    F    F    F    F    F    F
      
      0111 1111
      7    F
      
      1111 1111
      F    F
      
      0010 1010
      2    A
      
      0001 1011
      1    B
      
    4. given N bits, how many unsigned binary numbers can be represented?
      	0 to (2N - 1), or 2N numbers
      
    5. What is the result of adding these pairs of unsigned binary numbers? (ignore the carry bit for the leftmost bit of the result)
      1. 0001 + 1011
        0001
        1011
        ----- +
        1100
        
      2. 1011011 + 0110101
        1011011
        0110101
        -------- +
        0010000
        
  2. signed magnitude representation:
    1. What are the values of these signed magnitude binary numbers:
      1. 10000
        	-0
        
      2. 00000
        	+0
        
      3. 110110
        	-(16 + 4 + 2) = -22
        
      4. Given 7 bits to store signed magnitude numbers, how many numbers are there?
        The leftmost bit is for the sign, so only the remaining
        6 bits can be for the magnitude of the numbers.
        
        For the positives, there are:
        
        	000000 to 111111, (+0 to +63)
        	0 to 26 - 1,
        	which is 26 = 64 numbers
        
        For the negatives, there are also 64 numbers (-63 to -0).
        
        Overall there are 128 numbers that are represented.
        
        
  3. biased (excess) representation (ASSUME 7 BIT NUMBERS):
    1. What should the bias be?
      bias = 27 - 1 = 26 = 64
      
    2. What are these biased binary numbers?
      1. 1110110
        64 + 32 + 16 + 4 + 2 = 118 - bias = 118 - 64 = 54
        
      2. 0001011
        8 + 2 + 1 = 11 - bias = 11 - 64 = -53
        
      3. How many numbers are possible?
        0000000 to 1111111, or -64 to +63
        which is
        	-64 to -1 (64 numbers)
        	0 to +63 (64 numbers)
        or 64 + 64 = 128 numbers
        
      4. What is lost by using 7bit biased representation instead of 7bit signed magnitude representation?
        The biased representation can't store +64.
        And only has one representation for zero.
        
  4. two's complement representation (ASSUME 7 BIT NUMBERS):
    1. What are the smallest and largest numbers?
      smallest = 1000000 = -64
      biggest =  0111111 = +63
      
    2. And how many possible numbers are there ?
      There are 128 possible numbers:
      	-64 to -1
      	0 to +63
      
    3. Add the following numbers (assume last carry bits are ignored)
      1. 0101010 + 1010110
        0101010
        1010110
        -------- +
        0000000
        
      2. 0111111 + 1101011
        0111111
        1101011
        -------- +
        0101010
        
  5. real number representations:
    1. What is the decimal versions of these fixed point numbers?
      1. 111.111
        4 + 2 + 1 + 0.5 + 0.25 + 0.125 = 7.875
        
      2. -0.0101
        -(0.25 + 0.0625) = -0.3125
        
    2. Normalize the fixed point numbers above
      111.111 = 0.111111 * 23
      
      -0.0101 = -0.101 * 2-1
      
    3. Given a 10bit floating point representation (1 bit for sign of significand, 4 bits for the exponent (in the appropriate biased representation), 5 bits for the significand. Store the above fixed point numbers in this 10bit floating point representation.
      111.111 = [0 | 1011 | 11111]
      
      -0.0101 = [1 | 0111 | 01000]
      
    4. What are the closest numbers to zero in this 10bit representation, and what are the smallest and largest numbers possible?
      [0/1 | 0000 | 00000]
      	= (+/-) 0.1 * 2-8
      	= (+/-) 0.000000001
      	= (+/-) 2-9
      	= (+/-) 0.001953125
      
      [0/1 | 1111 | 11111]
      	= (+/-) 0.111111 * 2+7
      	= 1111110.0
      	= (+/-) 126
      
  6. Decimal to binary conversion:
    1. Run the following program and explain how its calculations work.
      #include <stdlib.h>
      #include <iostream.h>
      
      
      void main(int argc, char* argv[]) {
      
      	int get_positive_integer(int, char**);
      	void calculate_binary_number(int);
      	void display_usage_message(void);
      
      	int theInteger;
      
      
      	theInteger = get_positive_integer(argc,argv);
      	if (theInteger >= 0) {
      		calculate_binary_number(theInteger);
      		cout << endl;
      	}
      	else {
      		display_usage_message();
      		cout << endl;
      	}
      }
      
      void calculate_binary_number(int theInteger) {
      
      	int remainder = theInteger % 2;
      	int quotient = theInteger / 2;
      
      	cout << "For theInteger = " << theInteger
      	     << " remainder is " << remainder
      	     << " quotient is " << quotient << endl;
      
      
      	if ((remainder == 0) && (quotient == 0)) return;
      	else {
      		calculate_binary_number(quotient);
      		if (remainder == 1) cout << '1';
      		else cout << '0';
      	}
      }
      
      int get_positive_integer(int argc, char* argv[]) {
      
      	if (argc != 2) return -1;
      	else return atoi(argv[1]);
      }
      
      void display_usage_message(void) {
      	cout << "Usage: foo number" << endl;
      	cout << "number must be a positive integer"
      	     << endl;
      }
      
      The program takes a single command-line argument,
      checks that the argument is a positive integer,
      and then proceeds to calculate the binary representation
      for the positive integer.
      
      If there are too many arguments, or no arguments, or
      the argument is not a positive integer, a usage message
      is displayed. (Note that atoi returns 0 if the argument
      is not a number, also if the argument is a fixed point
      number, only the whole number part will be used.)
      
      The binary number is generated in reverse. As long as
      the quotient and remainder of theInteger are both not 0
      then recursion happens. The recursive calls are also
      before the cout statements for 1 and 0, thus the
      output will be reversed. But the binary number's digits
      are calculated in reverse; the net effect is an output
      of 0's and 1's for the binary number in normal order.
      
    2. Convert the following decimal numbers into unsigned binary:
      1. 1023
        1023 / 2 = 511 and 1, need 1 in the 20 bit
        511  / 2 = 255 and 1, need 1 in the 21 bit
        255  / 2 = 127 and 1, need 1 in the 22 bit
        127  / 2 =  63 and 1, need 1 in the 23 bit
        63   / 2 =  31 and 1, need 1 in the 24 bit
        31   / 2 =  15 and 1, need 1 in the 25 bit
        15   / 2 =   7 and 1, need 1 in the 26 bit
        7    / 2 =   3 and 1, need 1 in the 27 bit
        3    / 2 =   1 and 1, need 1 in the 28 bit
        1    / 2 =   0 and 1, need 1 in the 29 bit
        0    / 2 =   0 and 0, stop
        
        reverse this list, and get: 1111111111
        
      2. 27
        27 / 2 = 13 and 1, need 1 in the 20 bit
        13 / 2 =  6 and 1, need 1 in the 21 bit
        6  / 2 =  3 and 0, put  0 in the 22 bit
        3  / 2 =  1 and 1, need 1 in the 23 bit
        1  / 2 =  0 and 1, need 1 in the 24 bit
        0  / 2 =  0 and 0, stop
        
        reverse this list, and get: 11011
        
      3. 13.703125 (Hint: consider the whole and fraction separately)
        Consider 13:
        13 / 2 = 6 and 1, place 1 at bit 0
        6  / 2 = 3 and 0, place 0 at bit 1
        3  / 2 = 1 and 1, place 1 at bit 2
        1  / 2 = 0 and 1, place 1 at bit 3
        0  / 2 = 0 and 0, stop
        
        reverse this list, and get: 1101
        
        Consider 0.703125:
        0.703125 * 2 = 1 and .40625, place 1 at bit -1
        0.40625  * 2 = 0 and .8125,  place 0 at bit -2
        0.8125   * 2 = 1 and .625,   place 1 at bit -3
        0.625    * 2 = 1 and .25,    place 1 at bit -4
        0.25     * 2 = 0 and .5,     place 0 at bit -5
        0.5      * 2 = 1 and 0,	     place 1 at bit -6
        0	 * 2 = 0 and 0, stop
        
        use this list, and get: 0.101101
        
        
        overall 13.703125 = 1101.101101
        

Copyright © 1999, Jason Holdsworth. All rights reserved.