-1

I have this an unsigned int (uint16_t): 391 I want to flip all the bits around from right to left. That is to say, the rightmost bit becomes the leftmost bit.

I have tried x = 391 << sizeof(uint16_t) This seems not to work.

The desired output is: x = 57728

My current output is: x = 1564

Calvince
  • 37
  • 6
  • So what output did you get? [Edit] the question and make that clear, – Jabberwocky Nov 08 '22 at 09:57
  • 1
    Wire in two 16-bit ports, connect them together back-to-front, write to one, read from the other ) – Martin James Nov 08 '22 at 10:04
  • @MartinJames An demo will do, thanks – Calvince Nov 08 '22 at 10:05
  • ..or just brute-force it with a loop and shifting mask, probably quicker to write that code than asking this question ) – Martin James Nov 08 '22 at 10:06
  • @izlin No, it doesn't – Calvince Nov 08 '22 at 10:07
  • How do you expect `<< sizeof(uint16_t)` to flip any bit order? This is basically same as `* 4`. You need to look at the bits in one value and set bits accordingly in your result value. And as you need to do it for all 16 bits, a loop would be useful. – Gerhardh Nov 08 '22 at 10:22
  • You're going to have to do it one bit at a time. Do you know how to [extract and set individual bits](https://stackoverflow.com/questions/47981)? – Steve Summit Nov 08 '22 at 10:48
  • 1
    This was question was already marked as duplicate of [In C/C++ what's the simplest way to reverse the order of bits in a byte?](https://stackoverflow.com/q/2602823/694733). Don't know why it was reopened, but your answer is basically there in that question. – user694733 Nov 08 '22 at 10:51
  • C's shift operators `<<` and `>>` can turn the bit pattern `abcdefgh` into `defgh000` or `000abcde`. A "circular shift" operator (which C doesn't have built in, although it's a useful exercise to write one) can turn it into `defghabc` or `fghabcde`. But here you want `hgfedcba`, which is a completely different animal. – Steve Summit Nov 08 '22 at 10:55
  • @user694733: This question is about reversing the bits in a `uint16_t`, not about reversing the bits in a byte. The question I have now marked it as a duplicate of has answers covering 32 and 64 bits and a general case. – Eric Postpischil Nov 08 '22 at 11:02

1 Answers1

0

There is no C bitwise operator that does that, you have to implement it.

One way is to define a look-up table for some suitable amount of bits (8, or 4) and then chunk the value up and flip each chunk, then combine them together again.

Sounds like fun, let's code:

#include <stdint.h>

uint16_t flip(uint16_t x)
{
  const uint16_t nibbles[] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };

  uint16_t out = 0;
  for (int i = 0; i < 4; ++i)
  {
    const uint16_t here = nibbles[(x >> (4 * i)) & 15];
    out |= here << (4 * (3 - i));
  }
  return out;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 519 votes on [this answer](https://stackoverflow.com/a/746203/298225) suggest it is not all that uncommon. Bit-reversal is needed in some signal-processing algorithms, and some architectures provide an instruction for it. – Eric Postpischil Nov 08 '22 at 14:41
  • @EricPostpischil Good point, I removed the statement. Should have dupe-checked too, obvo. Thanks. – unwind Nov 08 '22 at 21:32