# Thread: Arithmetic operators to perform swap of nibbles.

1. ## 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. If index is from 0 to 15, then

pos = index << 4

3. 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. Originally Posted by DavidDos
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. 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.

6. Try this.
Code:
```    int index = 2;
int pos = index << 4;
long int mask = 0xf << pos;

7. 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.

8. 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