Quiz1 Review Questions
-
Convert powers of 2 to decimal and to hex. (See practice problem 2.2)
n 2n (decimal) 2n (hex) 4 16 0x10 5 32 0x20 64 0x100 20 -
Convert between decimal, binary, and hex representation of (non-negative) integers.
(See practice problem 2.3)
Decimal Binary Hexadecimal 35 0x4A 0110 1000 -
Evaluate the bit operations: |, &, ^, ~, <<, >> (logical and arithmetic), and ! on integers. (See practice problem 2.8)
Operation Result a 1110 1000 b 0000 1111 ~b !b a << 3 a ^ b a ^ ~b a & b -
Write a C expression in terms of the int variable x that yields a value equal to:
the least significant byte is unchanged and all other bits are set to 1.
Example: x = 0x00badfad, result = 0xffffffad
Ans: (~0 << 8) | xthe least significant byte is complemented and all other bits are set to 1.
Example: x = 0x00badfad, result = 0xffffff52
Ans: (~0 << 8) | ~xthe least significant byte is complemented and all other bits are left unchanged.
Example x = 0x00badfad, result = 0x00badf52,
Ans: (x & ~0xff) | (~x & 0xff)
-
(See practice problem 2.14) Fill in the following table in hex notation if x = 0x3C and y = 0x57
Expression Value Expression Value x & y x && y x | y x || y ~x | ~y !x || !y x & !y x && ~y -
C rules mandate that logical right shift is used for u >> n if the type of u is unsigned.
If x is an integer of type signed (char, short, int), C rules allow a compiler to use either logical right shift or arithmetic right shift for >> n.
However, C compilers generally all use arithmetic right shift for signed integers.
For byte integers and assuming C uses arithmetic right shift for signed integers fill in the table below using hex notation for these values of signed byte integer x (type char) and unsigned byte integer u(type: unsigned char):
char x = 0xC0 unsigned char u = 0xC0 Operation Value x << 1 u << 1 x >> 4 u >> 4 x >> 1 u >> 1 -
An 32 bit integer value 0x12345678 is stored at hex address 0xbfa9cf00.
If the machine is "big endian",
What byte is stored at 0xbfa9cf00?
What byte is stored at 0xbfa9cf01?
What byte is stored at 0xbfa9cf02?
What byte is stored at 0xbfa9cf03?
-
Repeat the previous problem if the machine is "little endian".
-
For the C program below, the begining output is shown. Fill in the rest of the output.
int main() { int a[4] = {10, 20, 30, 40}; int *p; p = &a[0]; printf("%p %d\n", p, *p); printf("%p %d\n", p + 1, *(p + 1)); printf("%p %d\n", p + 2, *(p + 2)); printf("%p %d\n", p + 3, *(p + 3)); return 0; }Output: 0xbfa4c6f0 10 -
In C, if x is of type int with initial value 0xdeadbeef, what is the value of x ^ x ^ x ? What is the value of x ^ ~x ^ x ?