Easy bit reversal

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.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 22일

1 개 추천

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

Oleg Komarov
Oleg Komarov 2012년 2월 22일
In the spirit of Cody: bin2dec(fliplr(dec2bin(10,32)))

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John F
John F 2012년 2월 23일

0 개 추천

Nice guys. thanks.
John F
John F 2012년 2월 23일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by