Link to home
Start Free TrialLog in
Avatar of prasad2315
prasad2315

asked on

bit change

i have binary number like this
1 0 1 1 1 0 1 0
i need to chage the bit value like  bit 8 to bit 1
                                                     bit 7 to bit2
                                                     bit 6 to bit 3
                                                     bit 5 to bit 4
when changed the binary number will become 0 1 0 1 1 1 0 1
how to implemnt this using the bit operators
Avatar of debuggerau
debuggerau
Flag of Australia image

depends on your libraries, but these should help.
You could use assembler, as this has rotate functions...

david.tribble.com/text/c0xrot.htm
www.devx.com/tips/Tip/14043
http://www.programmersheaven.com/mb/CandCPP/79561/79691/ReadMessage.aspx?S=B20000

Then I found this...
http://forums.devarticles.com/c-c-help-52/c-code-to-rotate-a-bit-in-a-given-number-50277.html

Hope that helps.
Avatar of ozo
The fastest way is probably with a 256 byte lookup table with all the reversed numbers pre computed
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prasad2315
prasad2315

ASKER

i need to he exchange the bit position like shown below
the bibnary number is
b7  b6  b5  b4  b3  b2  b1  b0
after the exchangeof the bits  the binary number will be
b0  b1  b2  b3  b4  b5  b6  b7
what will be the fastest implementation
>> what will be the fastest implementation

ozo already answered that. Which part do you want clarified ?
i want a generic implementation that wil work for all the numbers
>> i want a generic implementation that wil work for all the numbers

Both solutions shown by ozo are generic. The lookup table, as ozo said, is the fastest.
A table lookup will work for all the numbers if the table is correctly set up for all the numbers.
Thatis, if you mean by "all the numbers" all 8bit types. For larger types, the lookup table becomes impractical, and you should favor ozo's second solution (with a few minor changes to account for the increased size).