Filter löschen
Filter löschen

Easy bit reversal

37 Ansichten (letzte 30 Tage)
John F
John F am 22 Feb. 2012
I need a function to reverse the bits of an int. I see I can flip bytes around, but that doesn't reverse the bit order. Any easy ideas? Thanks.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Feb. 2012
sum(uint32(bitset(0,1:32,bitget(uint32(X), 32:-1:1))))
(yes, there are other ways, some of them quite short but not necessarily efficient.)
  1 Kommentar
Oleg Komarov
Oleg Komarov am 22 Feb. 2012
In the spirit of Cody: bin2dec(fliplr(dec2bin(10,32)))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

John F
John F am 23 Feb. 2012
Nice guys. thanks.

John F
John F am 23 Feb. 2012
I frown upon looping in Matlab, but this worked for me:
for i = 1:WordLength
h = bitshift(h,1) + bitand(x,1);
x = bitshift(x,-1);
end
x = the original number
h = the bit reversed version of x
If you put a bit of code in front of it to determine the WordLength, it works quite well.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by