Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reversing a binary number

Status
Not open for further replies.

navenmou

Full Member level 4
Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
Hi all

I want to write a C program for reversing a binary number i.e like this 001101---->101100.....I am not getting idea on how to proceed....please give me some ideas for that....

Thanks in advance
 

Re: Revrsing a binary number

Here's a quick and efficient algorithm to reverse the ordering of a byte:

Code:
unsigned char rbyte = 0;
unsigned char byte = 0xAA; // 0xAA Represents Value to Be Reversed

int i;

for (i=0 ; i<4 ; i++) 
rbyte |= ((byte & (1 << i)) << (7-2*i)) | ((byte & (1 << 7-i)) >> (7-2*i));

It works well.

BigDog
 
Re: Revrsing a binary number

Here's a quick and efficient algorithm to reverse the ordering of a byte:

Code:
unsigned char rbyte = 0;
unsigned char byte = 0xAA; // 0xAA Represents Value to Be Reversed

int i;

for (i=0 ; i<4 ; i++) 
rbyte |= ((byte & (1 << i)) << (7-2*i)) | ((byte & (1 << 7-i)) >> (7-2*i));

It works well.

BigDog

Another option would be


Code C - [expand]
1
2
3
4
5
6
7
unsigned char rbyte = 0;
unsigned char byte = 0xAA; // 0xAA Represents Value to Be Reversed
 
int i;
 
for (i=0 ; i<8 ; i++)
    if (byte & (1<<i)) rbyte |= (1<<(7-i));



I have tested it in AVRstudio and it executes at about 20% less cpu cycles

Alex
 
Re: Revrsing a binary number

If the bit reversal has to be carried out frequently in your code, a ROM table is the fastest solution, at least for 8 bit entities. For larger numbers, byte swap can be used in addition.
 
Re: Revrsing a binary number

This is an example of the lookup method suggested by FvM applied to 8-bit char and 16-bit integer, the lookup table is used for each nibble (4-bits)


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unsigned char reverse_byte (unsigned char input_byte)
{
  const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable
 
  return (lookup[input_byte & 15]<<4) | (lookup[input_byte>>4]);
}
 
 
 
unsigned int reverse_int (unsigned int input_int)
{
  const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable
 
  return (lookup[input_int & 15]<<12) | (lookup[(input_int>>4) & 15]<<8) | (lookup[(input_int>>8) & 15]<<4) | (lookup[(input_int>>12)]);
}



Alex
 
Revrsing a binary number Tested in Codevision 100% work,By Saeedvand. :)

void revise_data(char byte)
{
unsigned char hbyte,lbyte,d; // 0xAA Represents Value to Be Reversed
char revise,Final_revise_data;
int i;

revise=0;
lbyte=(byte)%(0x10);
hbyte=byte-lbyte;
for (i=0 ; i<4 ; i++)
revise |= ((lbyte & (1 << i)) << (7-2*i)) | ((lbyte & (1 << 7-i)) >> (7-2*i));
lbyte=revise;

revise=0;
for (i=0 ; i<4 ; i++)
revise |= ((hbyte & (1 << i)) << (7-2*i)) | ((hbyte & (1 << 7-i)) >> (7-2*i));

hbyte=revise;

Final_revise_data=hbyte+lbyte;

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top