Thread: Arithmetic operators to perform swap of nibbles.

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    Lightbulb Arithmetic operators to perform swap of nibbles.

    Hello all, I'm currently referencing THIRD EDITION COMPUTER SYSTEMS by BRYANT O'HALLARON for help to perform a swap, however i cant find information for what I'm attempting to accomplish.

    I have to Write a C program that will swap any two nibbles of a long int x (64-bit integer).A nibble is a four-bit aggregation. There are two nibbles in a byte. For this problemthe index of the least significant nibble is 0, and the index of the most significantnibble is 15 (so 0 <= m, n <= 15).

    So, if x = 0x0123456789ABCDEF (a 64-bitinteger), and if you swap two nibbles say that are at the 0th and 15th index, x wouldnow be x=0xF123456789ABCDE0.

    You are not allowed to use division, multiplication, or modulus, relativecomparisons (<, >, <=, >=), loops, switches, function calls, macros, conditionals (ifor ?

    You are allowed to use all bit level and logic operations, left and right shifts, additionand subtraction, equality and inequality tests, integer constants (<=255), INT_MINand INT_MAX, and casting between data types.



    I'm not sure how to accomplish this swap with all of the requirements listed above.

    How would i comparatively shift the value of a hexadecimal digit from least to most significant with only logic operators shifts and equalities?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    If index is from 0 to 15, then

    pos = index << 4
    mask = 0xf << pos

    That's your clue for today.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    4
    Hey, thanks for the response.
    I'm not quite sure what the clue is.
    Index is 0-15 Based on the given x = 0x0123456789ABCDEF. Thats correct.
    How would i traverse through the given X with bit level and logic operators to find the smallest value, and then the largest.
    From reading stackoverflow and the book, i understand i need "mask" the value so i can shift it rightwards", and then do the same with the largest value or index so i can shift it leftward.

    I see a response from someone stating:
    Adding an integer with 0xFF leaves only the least significant byte.

    I'm sorry, i just don't understand the process or where i can read about how masking and shifting works for something that would comply with these instructions.

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    242
    Quote Originally Posted by DavidDos View Post
    Hey, thanks for the response.
    I'm not quite sure what the clue is.
    Index is 0-15 Based on the given x = 0x0123456789ABCDEF. Thats correct.
    How would i traverse through the given X with bit level and logic operators to find the smallest value, and then the largest.
    From reading stackoverflow and the book, i understand i need "mask" the value so i can shift it rightwards", and then do the same with the largest value or index so i can shift it leftward.

    I see a response from someone stating:
    Adding an integer with 0xFF leaves only the least significant byte.

    I'm sorry, i just don't understand the process or where i can read about how masking and shifting works for something that would comply with these instructions.
    Following Salem's suggestion you can at least try to loop through (and print) each nibble of your variable. That would be a start.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    4
    I cannot perform loops within this program.
    Here's how I'm testing shifting and other operations.
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    
       long int x = 0x0123456789ABCDEF;
        
       printf("Size of long long int %ld\n", x);
       printf("Hex value = %llX\n", x);
        
       // pos = index << 4;
       // mask = 0xf << pos;
       
       x = x << 4;
        
       printf("Hex value shifted left = %llx\n", x);
       
       x = x >> 4;
        
       printf("Hex value shifted right= %llx\n", x);
        
    
    
        return 0;
    }
    I'm just having a really tough time getting started and understanding the process of masking and shifting.
    I performed a shift within the above code, if you run it you will see that a 0 when shifting left, and 4 last entries deleted when shifting right.
    I have been reading too much and getting no where.

    Only logical thing i can think of is i need to convert my HEX format of X into nibbles or 4 bits to represent each number.
    Then i somehow need to use those bits which (usually) the lowest will be 0000 and highest will be 1111, mask all bits except the chosen, and then shift the unmasked bit, then unmask all bits when shifting is completed.
    Last edited by DavidDos; 09-10-2018 at 03:18 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Try this.
    Code:
        int index = 2;
        int pos = index << 4;
        long int mask = 0xf << pos;
        
       x = x & mask;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2018
    Posts
    4
    O god i feel like an idiot. I thought i had to convert every character to binary and then mask the values with 0000 or 1111, but since 0 and f are the same hex representation it works the same way.
    Thanks for this. Now ill have to try messing around with my values in more detail now, Sorry that it took so long for me to understand something so simple, I hope i can move forward successfully now.
    One question i have from your example is, if 2 is <--- shifted left << 4, that would = 20000, so 0xf << right 20000 would not be possible, i do however see that you are somehow get 0 as the output.
    Last edited by DavidDos; 09-10-2018 at 06:32 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    My mistake.

    pos = index * 4
    There are 4 bits per nibble.

    In my effort to avoid multiplication, I missed a bit.
    * 4 should be written as << 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-09-2016, 12:28 PM
  2. Overloading arithmetic operators and commutativity
    By Ocifer in forum C++ Programming
    Replies: 14
    Last Post: 05-31-2011, 10:26 PM
  3. Replies: 6
    Last Post: 11-11-2009, 02:27 PM
  4. Replies: 25
    Last Post: 11-30-2008, 11:30 PM
  5. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM

Tags for this Thread