Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void printBits(unsigned x) {
- for (int i = sizeof(x) * 8 - 1; i >= 0; i--) {
- printf("%d", 1 & (x >> i));
- if (i % 4 == 0) {
- putchar(' ');
- }
- }
- puts("");
- }
- unsigned revNibbles(unsigned a) {
- const int nbNr = sizeof(a) * 2;
- const unsigned mask = (1 << 4) - 1;
- unsigned r = 0;
- for (int nbId = 0; nbId < nbNr; nbId++) {
- unsigned nb = (a >> (nbId * 4)) & mask;
- r |= (nb << ((nbNr - 1) * 4 - nbId * 4));
- }
- return r;
- }
- unsigned revNibBits(unsigned nb) {
- unsigned r = 0;
- for (int i = 0; i < 4; i++) {
- if ((nb & (1 << i))) {
- r |= 1 << (3 - i);
- } else{
- r &= ~(1 << (3 - i));
- }
- }
- return r;
- }
- unsigned revAllNibBits(unsigned a) {
- const int nbNr = sizeof(a) * 2;
- const unsigned mask = (1 << 4) - 1;
- unsigned r = 0;
- for (int nbId = 0; nbId < nbNr; nbId++) {
- unsigned nb = (a >> (nbId * 4)) & mask;
- r |= (revNibBits(nb) << (nbId * 4));
- }
- return r;
- }
- int main(void) {
- unsigned a;
- scanf("%u", &a);
- printBits(a);
- printBits(revNibbles(a));
- printBits(revAllNibBits(a));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement