Solution #1. E.g.
00001001 ==> 10010000
Here is the code,
#define GET(n, x) ((n&(1<<x))>>x)
#define SET(n, x) (n|(1<<x))
#define RESET(n, x) (n&(~(1<<x)))
00001001 ==> 10010000
Here is the code,
#define GET(n, x) ((n&(1<<x))>>x)
#define SET(n, x) (n|(1<<x))
#define RESET(n, x) (n&(~(1<<x)))
int reverseBits(unsigned char n) { int temp = n; int start = 0; int end = 7; while(start < end) { int sbit = GET(n, start); //find start bit int ebit = GET(n, end); //find end bit //set end bit equal to start bit temp = sbit?SET(temp, end):RESET(temp, end); //set start bit equal to end bit temp = ebit?SET(temp, start):RESET(temp, start); start++; end--; } return temp; }
No comments:
Post a Comment