Thread: Swap Nibbles

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Swap Nibbles

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    The goal is to "returns the value with the nibbles placed in reverse order", not to swap the first half of the bits with the second half of the bits (although that would be equivalent if there were only 8 bits, but unsigned int must have at least 16 bits to mathematically conform to the standard's requirements on minimum ranges).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Quote Originally Posted by laserlight View Post
    The goal is to "returns the value with the nibbles placed in reverse order", not to swap the first half of the bits with the second half of the bits (although that would be equivalent if there were only 8 bits, but unsigned int must have at least 16 bits to mathematically conform to the standard's requirements on minimum ranges).
    Well I thought about it as being a 32-bit number, and the first half would 16 bits and the second half the other 16 bits. Wouldn't it work this way?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    224
    Quote Originally Posted by rmmstn View Post
    Well I thought about it as being a 32-bit number, and the first half would 16 bits and the second half the other 16 bits. Wouldn't it work this way?
    A nibble is always four bits. So a 32 bit value has 8 nibbles.

    Here's a hint:

    Code:
       unsigned a = 0
       a = (a << 4) + 1;
       a = (a << 4) + 2;
       a = (a << 4) + 3;
       a = (a << 4) + 4;
       a = (a << 4) + 5;
       a = (a << 4) + 6;
       a = (a << 4) + 7;
       a = (a << 4) + 8;
    What is a (in hex) at the end of this code?
    Last edited by hamster_nz; 11-01-2020 at 02:29 PM.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Quote Originally Posted by hamster_nz View Post
    A nibble is always four bits. So a 32 bit value has 8 nibbles.

    Here's a hint:

    Code:
       unsigned a = 0
       a = (a << 4) + 1;
       a = (a << 4) + 2;
       a = (a << 4) + 3;
       a = (a << 4) + 4;
       a = (a << 4) + 5;
       a = (a << 4) + 6;
       a = (a << 4) + 7;
       a = (a << 4) + 8;
    What is a (in hex) at the end of this code?
    Code:
    unsigned a = 0;
        a = (a << 4) + 1; //a = 0 * 2^4 + 1 = 1
        a = (a << 4) + 2; //a = 1 * 2^4 + 2 = 18
        a = (a << 4) + 3; //a = 18 * 2^4 +3 = 291
        a = (a << 4) + 4; //a = 291 * 2^4 + 4 = 4660
        a = (a << 4) + 5; //a = 4660 * 2^4 + 5 = 74565
        a = (a << 4) + 6; //a = 74565 * 2^4 + 6 = 1193046
        a = (a << 4) + 7; //a = 1193046 * 2^4 +7 = 19088743
        a = (a << 4) + 8; //a = 19088743 * 2^4 + 8 = 305419896
        printf("0x%x", a); //12345678

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arithmetic operators to perform swap of nibbles.
    By DavidDos in forum C Programming
    Replies: 7
    Last Post: 09-10-2018, 06:56 AM
  2. Swapping nibbles
    By vinoth14 in forum C Programming
    Replies: 5
    Last Post: 02-01-2016, 06:16 AM
  3. swapping nibbles
    By rits in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:24 AM
  4. Nibbles program...
    By sreeramu in forum C Programming
    Replies: 2
    Last Post: 10-22-2007, 06:22 AM
  5. IDEA: Nibbles
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-02-2002, 05:24 PM

Tags for this Thread