Computer programming - Lab 5

1. Print bits Write a function that prints the bits of an unsigned n, without any excessive leading zeroes.

2. Reverse bits Write a function that given an unsigned n returns the value with the bits reversed
a) reversing the full bit width of n (the LSB becomes the MSB);
b) reversing the minimum bitlength that fits the initial number (the most significant 1 bit becomes the LSB).

3. Nibbles A nibble is a group of 4 bits. Write a function that given an unsigned n
a) returns the value with the nibbles placed in reverse order
b) returns the value with the bits in each nibble reversed

4. Consecutive bits Write a function that given an unsigned n returns the number of segments of consecutive equal bits in the binary representation of n. Example: 000100 has three segments: 000, 1, 00.

5. Find bit pattern Write a function that counts how many times the pattern formed by the low-order k bits in the representation of p can be found in the representation of n (including overlapping occurrences).

6. Integer part Write a function that computes the integer part of a float by examining the bits of its representation.
To load the bit pattern of a float f into an integer, use: unsigned u = *(unsigned *)&f;

7. Bitwise addition Write a function that adds two numbers, bit by bit, using the elementary school algorithm with carry (from LSB to MSB).

8. Bitwise multiplication Write a function that multiplies two unsigned 32-bit integers, returning a 64-bit unsigned. Iterate over each bit of one number. You may use addition.

9. Polynomial division in Z2 A polynomial with with coefficients 0 or 1 can be represented as an integer (each bit is the coefficient for the corresponding power).
a) Write a function that divides two polynomials and returns the result (all represented as above). b) A sequence of bytes read from input can be viewed as a polynomial, with most significant coefficient first. Write a function that divides this polynomial (read until end of input) by a given 32-bit polynomial, and returns the remainder. This is used in various CRC computations.

Marius Minea
Last modified: Tue Oct 21 17:50:00 EEST 2014