• C

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
prasad2315Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

debuggerauCommented:
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.
0
ozoCommented:
The fastest way is probably with a 256 byte lookup table with all the reversed numbers pre computed
0
ozoCommented:
Another nice way is
value = (value << 4) | (value>>4);
value = ((vaue&0x33)<< 2) | ((value>>2)&0x33);
value = ((vaue&0x55)<<1) | ((value>>1)&0x55);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

prasad2315Author Commented:
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
0
Infinity08Commented:
>> what will be the fastest implementation

ozo already answered that. Which part do you want clarified ?
0
prasad2315Author Commented:
i want a generic implementation that wil work for all the numbers
0
Infinity08Commented:
>> 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.
0
ozoCommented:
A table lookup will work for all the numbers if the table is correctly set up for all the numbers.
0
Infinity08Commented:
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).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.