Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- }
- putchar('\n');
- }
- /* 2. a) Write a function that takes a 32-bit unsigned
- * and returns the 16-bit number formed by the bits in
- * even positions.
- * I.e., if we label the bits. 0xBA9876543210, return the
- * number formed by bits0xA86420.
- * b)The same problem, but place the bits in reverse order.
- */
- unsigned reverseBits (unsigned n){
- unsigned result = 0, bitSize = sizeof(unsigned) * 8;
- int i;
- for (i = 0; i < bitSize; i++){
- result = result | (1 & n);
- n = n >> 1;
- if(i + 1 < bitSize){
- result = result << 1;
- }
- }
- return result;
- }
- unsigned evenPositions32b (unsigned n){
- unsigned result = 0;
- int i;
- for (i = 0; i < 16; i++){
- result = result | ((1 << i) & n);
- n = n >> 1;
- }
- return result;
- }
- int main(){
- int a = 0xAF;
- printf("even positions on a reversed would be %x\n", reverseBits(evenPositions32b(a)));
- printf("even positions on a would be %x", evenPositions32b(a));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement