Exam Practice Q's

  1. What is the 8-bit binary 2's complement representation of the decimal value -37?
1101 1011
  1. Given the function declaration: int numUChars(char * str); This function returns the number of upper case characters in the string str. Write the body of this function.
#include <string.h>
int numUChars(char * str) {
    int n = 0;
    while (*str) {
        if (str >= 'A' && str <= 'Z') {
            n++;
        }
        str++;
    }
    return n;
}
  1. Write a C function that takes an unsigned integer parameter and returns 2.5 times that value without using any multiplication or division operators. For example, if the parameter has a value of 16, the result should be 40.
int times2point5(int x) {
    return (x << 1) + (x >> 1); (2^1 + 2^-1)
}
FB = (X >> 4) & 3;
X &= 0xFE0000FF;
X |= (C & 0x1FFFF) << 8;
struct response {
    int fld_a:4;
    int fld_b:2;
    int reserve:2;
    int fld_c:17;
};
-1100.11 * 2^0 == -1.10011 * 2^3 (shift to the left 3)
Sign: 1
Mantissa: 10011000000000000000000
Exponent: 127 + 3 == 130
0xc14c0000
0x12345678 -> 0001 0010 0011 0100 0101 0110 0111 1000
-> 0 010 010 001 101 000 101 011 001 111 000
-> 2 215 053 170
weight in carat with 4 bits to the right of the decimal point
this gives range up to 4096
milligrams -- 200 milligrams is one carat
carat scale is most common scale for diamonds
range is little larger than largest diamond ever found: 3167 carats
0x0001 is 1/16 of a carat
0xffff is 4095.9375 carats