Reverse (Mirror) Binary Number

C

Colmeister

Hi,

This should be a simple task but I seem to be having a brain-block at the
moment.

I have a 6 bit number (0 - 63) that I need to reverse at the binary level,
e.g.

If the starting number is 5
The binary representation is 000101
The reversed binary representation is 101000
The result (which I need to be able to calculate) is 40

I can do this by converting the number to a string (or char array)
representation of the binary number, reversing it, and then converting back
to a number, but there must be a better method.

Thanks in advance,

Colin
 
P

Peter Morris

Written in my news reader so might not work

public byte ReverseBits(byte source)
{
byte result = 0;

byte sourceBit = 1;
byte targetBit = 128;
for (int i = 0; i < 8; i++)
{
if (source & sourceBit == sourceBit)
result |= targetBit;
sourceBit = sourceBit << 1;
targetBit = targetBit >> 1;
}

return result;
}

It's a template at least :)


Pete
 
A

Alberto Poblacion

Colmeister said:
This should be a simple task but I seem to be having a brain-block at the
moment.

I have a 6 bit number (0 - 63) that I need to reverse at the binary level,
e.g.

If the starting number is 5
The binary representation is 000101
The reversed binary representation is 101000
The result (which I need to be able to calculate) is 40

If you know that your number is always going to be 6 bits long, you can
simply move each bit to its fixed position:

int num = 5;
int result = ((num & 32) >> 5) | ((num & 16) >> 3) |
((num & 8) >> 1) | ((num & 4) << 1) | ((num & 2) << 3) | ((num & 1) << 5);
 
A

Arne Vajhøj

Colmeister said:
This should be a simple task but I seem to be having a brain-block at the
moment.

I have a 6 bit number (0 - 63) that I need to reverse at the binary level,
e.g.

If the starting number is 5
The binary representation is 000101
The reversed binary representation is 101000
The result (which I need to be able to calculate) is 40

I can do this by converting the number to a string (or char array)
representation of the binary number, reversing it, and then converting back
to a number, but there must be a better method.

I would use a lookup table with 64 elements.

If you absolutely need to calculate to confuse readers of the
code then use:

// must be compiled without check for integer overflow
public static byte Reverse(byte b)
{
return (byte)(((b * 0x80200802UL) & 0x0884422110UL) *
0x0101010101UL >> 34);
}

Arne



Arne
 
J

J.B. Moreno

Colmeister said:
Hi,

This should be a simple task but I seem to be having a brain-block at the
moment.

I have a 6 bit number (0 - 63) that I need to reverse at the binary level,
e.g.

If the starting number is 5
The binary representation is 000101
The reversed binary representation is 101000
The result (which I need to be able to calculate) is 40

Give ~ (not) a try...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top