Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

BitReverse Assignment for C Programming

M. Beeson

The Assignment

The assignment is to write a function void BitReverse(unsigned *x) that reverses the bits of *x. For example, if n = 5, the bits of n are 00000000000000000000000000000101 and after we call BitReverse(&n), the bits of n should be 1010000000000000000000000000000 Explicitly, to reverse the bits of n means to make the new value of the i-th bit be the old value of the 32i1-th bit. (Assuming unsigneds are 32 bitshowever, in your code use 8*sizeof(unsigned) rather than 32, so your code does not assume anything about the size of the basic types of C.) As usual, you should put your program in three les, BitReverse.c, BitReverse.h, and main.c. You will submit only BitReverse.c. You must do this assignment without using strings in any way in BitReverse.c. This is an exercise in bitwise operations. (You can use anything you want to in your testing code in main, of course.)

Programming hints

Use what you learned in class about how to read a bit, set a bit, and clear a bit. Macros for doing those things were given in class; you can use them.

Testing your program

You will want to write a function bitprint(unsigned n) that prints out the bit pattern of an unsigned. That way, you can print out the bit pattern of an integer n, assign m = n, call BitReverse(&m), and then print out the bit patterns of n and m to see if they are the reverse of each other. Be sure to test some numbers with the highest-order bit set, not just small numbers.

You might also like