Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Write a function that converts a binary string to a numeric value. That is, if you have

     char * pbin = "01001001"; 

    then you can pass pbin as an argument to the function and have the function return an int value of 25 .

  2. Write a program that reads two binary strings as command-line arguments and prints the results of applying the ~ operator to each number and the results of applying the & , , and ^ operators to the pair. Show the results as binary strings.

  3. Write a function that takes an int argument and returns the number of ON bits in the argument.

  4. Write a function that takes two int arguments: a value and a bit position. Have the function return 1 if that particular bit position is 1, and have it return 0 otherwise .

  5. Write a function that rotates the bits of an unsigned int by a specified number of bits to the left. For instance, rotate_l(x,4) would move the bits in x four places to the left, and the bits lost from the left end would reappear at the right end. That is, the bit moved out of the high-order position is placed in the low-order position.

  6. Write a program with the same functionality as Listing 15.3. However, instead of using a structure with bit fields, it should use an unsigned int and the bitwise operators.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net