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.

[SOLVED] reverse a binary number

Status
Not open for further replies.

Marvin_G

Junior Member level 3
Joined
Nov 21, 2011
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,485
what is the simplest code to reverse a binary number in c? like 11110000 --> 00001111..i got 1 but its quite long..mind to share ur idea? ^^
 

A lookup table, this was suggested by FvM in an older post about FPGA I think but it applies to mcu too.

The fastest way would be to use 8bit lookup table but you need 256 bytes of data.
I have used a 4bit lookup table that makes the conversion in two steps with just 16bytes for the lookup table, it is the best compromise I think.

See the 8 and 16bit functions 8/16 bit value reverting function (11000000 to 00000011) - Blogs - Forum for Electronics

Alex
 

u have to rotate the bits so.... but not just shift...

to rotate a byte by n bits to the right:

a = (a >> n) || (a << (8-n))

you probably want to make your code more general (don't hardcode '8').​
 

For 8051 MCUs:

....
....
MOV A, byte
MOV counter, #8
LOOP:
RLC A
XCH A, temp
RRC A
XCH A, temp
DJNZ counter, LOOP
....
....

The register "temp" holds the inverse of "byte"

Kerim
 
Last edited:

thanks guys for your idea..^^

---------- Post added at 04:06 ---------- Previous post was at 04:05 ----------

For 8051 MCUs:

....
....
MOV A, byte
MOV counter, #8
LOOP:
RLC A
XCH A, temp
RRC A
XCH A, temp
DJNZ counter, LOOP
....
....

The register "temp" holds the inverse of "byte"

Kerim

hye there..tanks for ur code but i wanted it in c..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top