Hacker News new | ask | show | jobs
by jtchang 3080 days ago
Do you mean a word by 8 bits? Or 16 or whatever the architecture defines as a word?

So 1000 gets flipped to 0001 ?

1 comments

It was a uint32_t in this case (we do a bunch of ARM stuff mainly so without any other context, that's what 'word' means to me). And yeah, exactly, 1000 to 0001. We had an environment setup with a bunch of test cases and a stub function to fill in. When you come in, push the button, everything compiles, but most of the tests fail because the function is just

    uint32_t flip_bit_endianess(uint32_t word) {
      return word;
    }
I think there's actually a couple passing tests initially that flip to the same thing.

And like I said, this is for an experienced embedded software position. Ironically enough I care less about showing that you can code in junior positions. Those are more "does it seem like you can learn?"

Is there an efficient solution to this? I'm relatively used to switching byte orders, but I've never encountered a situation where I needed to flip bit orders before. I could certainly do it with a bunch of shifts and ors, but that seems awfully inelegant for an interview question. (A quick search indicates that there isn't any instruction that makes this easier than bit twiddling, but I'm not sure if I'm missing something.)

Where would this be used in the real world?

> Is there an efficient solution to this?

To be real, I'm not even looking for a particularly efficient solution; just "can you make the tests all turn green with ors and shifts" is what I'm looking for.

There is a O(log(N)) N is the number of bits solution where you swap each pair of bits, and then swap pair of pairs, and then each pair of pair of pairs, and so on. I want to say that we clocked it to 15 cycles for a 32 bit word on a semimodern superscalar core that can do each half of each pair in parallel until you or them back together.

All of that being said, I'm really just looking for a for each bit, shift and or it into the right place, O(N) solution. It's mind boggling how many people with wonderful resumes can't do that in an interview time block. Hence why I don't have a lot of patience for the whole "well I'm senior and have a great resume, so you shouldn't have to make me do a stupid coding thing that's beneath me" mindset.

> Where would this be used in the real world?

Yeah the problem itself is a little contrived, but it's a good use of the sort of logic and putting the pieces in the right place at the right time sort of skills you use in embedded programming, and more so HDL, which we require of our software engineers too. And I have seen it in the real world a few times on a serial line that has the wrong bit endianess.

I haven't used bitwise operators since college (approaching 8 years ago) but wanted to try this as a fun exercise during the NFL playoff game. Here's what I came up with -- mostly untested.

[edit] Note this is obviously for 16-bit integers.

int flip_endian_helper(int num, int i) {

    if (i == 16) {
     return 0;
    }

    int shift = 15 - 2*i;
    int workingBit = num & (1 << i);
    int partial = shift > 0 ? workingBit << shift : workingBit >> (shift * (-1));
    
    return partial | flip_endian(num, i + 1);
}

int flip_endian(int num) {

  return flip_endian_helper(num, 0);

}