A nibble is a group of 4 bits. Write a function that given an unsigned n returns the value with the nibbles placed in reverse order.
Code:
#include <stdio.h>
unsigned swap_nibbles(unsigned n){
unsigned newNumber = 0;
unsigned nBits = sizeof(unsigned) * 8;
unsigned firstHalf = n << (nBits / 2);
unsigned secondHalf = n >> (nBits / 2);
newNumber =
return newNumber;
}
int main(){
unsigned a = 0x5A;
printf("The initial number is %d\n", a);
printf("The new number with the nibbles swapped is %x", swap_nibbles(a)); //A5000000000000
return 0;
}
I need to know what bitwise operations should I use, in order to switch firstHalf and secondHalf in newNumber (my guess would be XOR), but I don't know how I could write this.