| > 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. |
[edit] Note this is obviously for 16-bit integers.
int flip_endian_helper(int num, int i) {
}int flip_endian(int num) {