Computer programming - Lab 5

1. 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

2. 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.

3. Compare numbers Write a function that compares two signed integers by examining their individual bits starting with the most significant. Return -1 if the first int is smaller than the second, 1 if it is larger, and 0 if they are equal.

4. 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).

5. 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;

6. Floating-point addition Write a function that adds two positive float numbers by processing their bit representation. Find the larger of the two exponents and shift the other mantissa to align it to the same decimal point, then add the mantissas, and do any rounding or exponent correction.

7. Floating-point inverse Write a function that given a nonzero float x returns 1/x manipulating its bit representation. Use step-by-step division with the mantissa.

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

9. 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.

10. 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) The bits in 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: Thu Oct 29 6:30:00 EET 2015