Hi, I am trying to write a function in C that takes a single character, converts it to binary, and then reverses the order of bits. I had originally tried it using putchar, but the function is not allowed to print anything, simply return the reversed bits. I am wondering if anyone is able to offer any help, or provide an example of how I could do this, thanks! This must be done with bitwise operations.
**Also if this is not posted in the correct spot please let me know, I wasn't sure where else to post, thanks!**
Hi J - without giving a full answer, you need a way of:
extracting bits from a number
constructing a number from bits
One way to extract bit number X from value A is:
To set bit X in value A (assuming it is currently 0):
So you could combine these in a loop, extracting a bit from your input value and setting the appropriate bit in your output (reversed bits) value. You just need to work out how to calculate 'appropriate'.