4

how do you reverse and rotate hex numbers and return the number in C with bitwise operators?

for example:

0xabcd -> 0xdcba

0xabcd -> 0xdabc
1

4 Answers 4

13

It's hard to know where to begin with this question. Plus I smell homework.

Some points:

  • There is no such thing as a "hex number". Hex is just a notation. How do you reverse and rotate decimal numbers and return the number in C? For example:

    1776 -> 6771

    1776 -> 6771?

  • To solve this problem, you need a deep understanding of positional notation, whether it's base 10, base 16, base 2, or what have you.

  • All you need can be had by adding, subtracting, multiplying, and dividing. Those are operations on numbers. Modulus is also very helpful.

  • If you happen to want to multiply or divide by a power of two, I commend to you the C left shift << and right shift >> operators. These work perfectly for numbers that are represented using the C types unsigned or unsigned long.

4
  • 6
    +1 for explaining (in an appropriately cursory fashion) the distinction between the number and its notation in a particular base. Commented Jan 13, 2010 at 4:05
  • Really? I get annoyed when people assume you don't know the definition. Just because you used a word that is semantically wrong does not mean you don't know what it is.
    – Louis
    Commented Jan 13, 2010 at 4:59
  • @Louis: I don't think that Norman is implying that he doesn't know what it is, I think he's trying to make the distinction more clear. The fact that "10" in decimal is "A" in hex but they're the SAME, and so "switching" the number 123 in hex is going to be a mathematically different operation than switching 123 in decimal is not immediately obvious to most people. Commented Jan 13, 2010 at 5:41
  • Oh yeah I wasn't talking about this particular case. I just find in general that a lot of responses end up being definitions if you used a wrong term.
    – Louis
    Commented Jan 14, 2010 at 0:33
1

Hex numbers are numbers, as Norman's answer points out. However, 1 hex digit = 4 bits, so these operations actually make sense as things you could possibly want to do to integer values.

The 2nd one is a bitwise rotation by 4 bits. See Best practices for circular shift (rotate) operations in C++ for best practices for compiler-friendly rotates that guard against C/C++ undefined-behaviour.

If your input isn't 8, 16, 32, or 64 bits, then you might need to shift + mask manually, instead of relying on shift-in-zeroes.


The first one will require more code: it reverses the order of the nibbles. There is no machine instruction for that, or easy way to build it out of a few bitwise operations on the entire number at once.

I think you'd have to reverse the order of the bytes, and then reverse the order of nibbles within each byte (an 8-bit rotate by 4).

0

For fun, follows in a recursive solution that works for any width of digits.

#include <limits.h>
unsigned ReverseHex(unsigned x, unsigned DigitWidth) {
  if (DigitWidth <= 1) {
    return x;
  }
  unsigned SideDigitWidth = DigitWidth / 2;
  unsigned SideBitWidth = SideDigitWidth * 4;
  unsigned CenterAndRightDigitWidth = DigitWidth - SideDigitWidth;
  unsigned CenterAndRightBitWidth = CenterAndRightDigitWidth * 4;
  unsigned CenterAndRight = x & ((1u << CenterAndRightBitWidth) - 1);
  unsigned Right = x & ((1u << SideBitWidth) - 1);
  unsigned Center = CenterAndRight - Right;

  return ReverseHex(x >> CenterAndRightBitWidth, SideDigitWidth) + Center
      + (ReverseHex(Right, SideDigitWidth) << CenterAndRightBitWidth);
}

int main(void) {
  printf("%X\n", ReverseHex(0x1234, 4));
  printf("%X\n", ReverseHex(0x12345, 5));
  printf("%X\n", ReverseHex(0x1234567, 7));
  printf("%X\n", ReverseHex(0x12345678, 8));
  return 0;
}

Output

4321
54321
7654321
87654321
-1

To swap the number using bit operations:

Do a bitwise AND operation using the original number with appropriate mask to extract a hex digit (4 bits) from the original number.

Shift this extracted bit pattern to it's new location.

Bitwise OR the repositioned bit patterns together.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.