CSC373 Quiz 1 Answers
Answers are in red.
-
Convert between decimal, binary, and hex representation of (non-negative) integers.
Decimal Binary Hexadecimal 31 0001 1111 0x1F 95 0101 1111 0x5F 90 0101 1010 0x5A -
Evaluate the bit operations: |, &, ^, ~, <<, >> (arithmetic right shift), and ! on integers.
Operation Result a 0x00EE 00FF b 0x8800 66AA a & b 0x0000 00AA a | b 0x88EE 66FF ~b 0x77FF 9955 !b 0x0000 0000 a << 3 0x0770 07F8 b >> 4 0xF880 066A a ^ b 0x88EE 6655 a ^ ~b 0x7711 99AA -
Write a C expression in terms of the int variable x with result:
The most significant byte is unchanged and all other bits are set to 0.
Example: x = 0x56789ABC, result = 0x56000000
Assign the expression to the int variable result.
Rules:
- Use only declarations of int variables and assignment statements.
- Allowed operators: & | ^ << >> + !
- Allowed constants: one byte integers. E.g. 0xFF.
result = (~0 << 24) & x; or result = (0xFF << 24) & x; or result = (x >> 24) << 24;
Challenge Problem (5 pts extra Credit)
-
Write a C expression in the int variable x that equals ~x if x is < 0 and equals x if x >= 0. Assign the expression to the int variable result.
Rules:
- Use only declarations of int variables and assignment statements.
- Allowed operators: & | ^ << >> + !
- Allowed constants: one byte integers. E.g. 0xFF.
result = (x >> 31) ^ x;