Inferno Programming Forums

Full Version: [FAST|MEDIUM] Bit Reversal Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Bit Reverse function, that you can code in any language (perferably one that can access BITS like C#, C, Assembly).

C++ prototype:
int BitReverse(int binary);

Let's see your implementation.

Example:
11100011001001
--->BitReverse-->
10010011000111

Prize: 20 Rep points.
What's up with the FAST in the title?
You mean the algorithm has to be fast?
No I mean the challenge could END before you finish. (Reason is, I'm coding it too and making it a tutorial).
I'd say you have a week at most.
Well... Here's a straightforward way of doing it. I'll think about other possibilities later. I'm pretty sure there are quite some AND/OR/XOR/SHIFT hacks that you can use to implement a rotating shift, which is what this program basically does. A full rotated [strike]Censored[/strike] shift to the left or right...
PS: eXecute, could you please add .cpp .c and some other useful extensions to the attachment filter? I had to change the extension of the file to post it...
Did you program all this? Because that REALLY WAS fast.... almost... too fast!
Especially for someone who I thought was only beginning college and doesn't have Comp Eng, Bit training :O.

Seriously though good job!

My function is a lot different...

I'll fix filter.
Haha LOL 001_tongue
I was wondering whether you'd be impressed or not. I kind of messed with bitwise operations lately so that's why I was able to do it so fast! Laugh
Plus, I've been looking into the other challenge (add numbers using bitwise operations) and already figured it out how to add the numbers in binary, but I'm having some minor problems when it overflows. I want it to rotate, as in 65535 + 1 = 1 (for 16 bits) instead of the weird stuff I'm getting. I found a nice method of adding in binary but I'm having some problems understanding it fully.
I used your code to display it, I hadn't finished it when I posted it, but I had a theory Laugh.

I don't think it works right now, but in theory it's suppose to work.
Right now I'm debugging it with your code Laugh (I like your functions!!),

Check mine out, maybe you'll solve that as well, but I got packing to do.

Btw you clearly are the winner of this challenge, but I'm still going to try and solve mine.
Oh Censored your code look hackish! 001_tongue A lot of hex values I like it. I'll look over it after I buy dinner 001_tongue
BRB!
Hm, I don't understand your algorithm. Could you please explain what you're trying to do? Are you trying to rotate the bits to the left/right? And carrying the rightmost/leftmost bit to the left/right?
New challenge please -.- one that wil *not* be solved before I read it D:
Ha, I agree with Richard! I had this done too, I had to do this for my programming class years ago. 001_tongue
Here's my bit reversal. Sorry it took forever.

But I can only do 4 hexes at a time! 0xA0E3 is input.

    C++ Programming
#include <iostream>
using namespace std;
 
 
 
template<class T> void
printBinary(T num) {
	for (int i = sizeof(num)*4-1; i >= 0; i--) {
		cout << bool(1 & (num >> i));
	}
	cout << endl;
}
 
 
int main(){
  int iInput = 0xA0E3;
  cout << "Input: ";
  printBinary(iInput);
  int iFullCode(0);
  int iBitCount(0);
  for(int iBits = 0; iBits < (sizeof(iInput)*4-1); iBits++){
      iFullCode += (iInput << (iBitCount));
      iBitCount++;
  }
  cout << "\nReverse: ";
  printBinary(iFullCode);
  return 0;
}

Uhm... I get this as output for your program...
Quote:Input: 1010000011100011
Reverse: 1101111100011101

But Reverse should be 1100011100000101.
I'm not sure I understand how you're using addition and shifting to do this.

Quote:But I can only do 4 hexes at a time! 0xA0E3 is input.
You mean only 16 bit variables? That's because both you and I did the same mistake for some reason when we determined the max offset of a bit. It's not sizeof(iInput)*4-1, it's sizeof(iInput)*8-1 since sizeof() returns the numbers of bytes and a byte has 8, not 4, bits. Doh.
Good catch, didn't notice before.

Yeah I did do *8 before, it got lengthier but I when I saw the reverse mess up I thought I did something wrong and undo-ed, but I know something is wrong with the code now. (It looked right to me when I posted)

And the shifting does work, just not the code I posted above. I used it in another project.
The problem with this one is, it would work if it was getting iInput as 1 bit at a time, and it's not (the mistake is in my other project it is).

So, it needs:
- mask
- size declared outside for performance

    C++ Programming
unsigned int iFullCode(0);
  unsigned int mask(0x1);
  unsigned int size = (sizeof(iInput)*4-1);
  cout << size << endl;
  for(unsigned int iBits = 0; iBits <
size; iBits++){
    iFullCode |= ((iInput & mask) << (size - iBits));
    printBinary(mask);
    printBinary(iFullCode);
    mask <<= 1;
  }



Also, sometimes binaries work better with unsigned, no need to worry about signs, but it should work either way.

It's still not working properly, but I have no clue why, it's suppose to do it right.

Btw alinush if you have gtalk (or gmail or pidgon or whateva), add me! (PM me if you don't know).
Hm, I don't have time to debug your code... can you explain the algorithm you're trying to implement?
this is what's suppose to happen
iInput
1010110111011
FullCode:
1000000000000
1100000000000
1100000000000
1101000000000
1101100000000
1101110000000
1101110000000
1101110100000
1101110110000
1101110110000
1101110110100
1101110110100
1101110110101
Mask:
0000000000001
0000000000010
0000000000100
etc etc
Reference URL's