Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Bitwise reversal of a hexidemal number?
Page 2 of 2 | |||||
Author | Message | ||||
drkl Senior Member Joined: 18/10/2015 Location: HungaryPosts: 102 |
Helo, TassyJim's solution is the best (by my opinion...). It operates every lenght: main: INPUT "BIN NUM, LENGHT(2...): ",s$, h s$="&b"+s$ PRINT bin$(val(s$,h),h)," rev: ", bin$(bitreverse(val(s$),h),h) GOTO main FUNCTION bitreverse(num, k) ' reverse bit order ' num = integer to reverse ' k = number of bits (8 or 16) LOCAL n FOR n = 1 TO k IF (num AND 2^(n-1)) > 0 THEN bitreverse =bitreverse+ 2^(k-n) ENDIF NEXT n END FUNCTION |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1141 |
Hi Kónya, this is not what TassyJim says! I really appreciate Jim and his contributions here! Always good stuff! He limitated the lenght for 8 or 16 (and it worrks well!). ' k = number of bits (8 or 16)
But his code is not working for big negative 64bit integers (eg -1) and a lenght of 64 for example! Note: the CFunction can be up to >100 times faster! num (dec):-1 lenght: 64bit
1111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111111111111 rnum (dec):-1 100 µs for one borev function call 1111111111111111111111111111111111111111111111111111111111111110 rnum (dec):-2 18242 µs for one bitreverse function call Sometimes it depends on speed! Thanks for your comment! Michael |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5923 |
The bitreverse function was written well before we had integers (and CFunctions) to play with. Assuming that we wanted to work in bytes, 16 bits is the max that we can use with 32bit floating point numbers. That is where the '8 or 16' number of bits came from. Jim VK7JH MMedit  MMBasic Help |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1141 |
Yes, that's true! I do not hope that you feel any kind of criticism!? The C-code is very similar to your Basic code. As anybody can see. Regards Michael |
||||
ajkw Senior Member Joined: 29/06/2011 Location: AustraliaPosts: 290 |
I thought I would do a bit of speed testing and it seems using a IF statement is marginally quicker. However it also seems it is even quicker if you drop the >0 test IF (num AND 2^(n-1)) > 0 THEN ==> IF (num AND 2^(n-1)) THEN The >0 test is not needed as (num AND 2^(n-1)) returns 1 or 0 / true or false which is all the IF statement needs. Cheers, Anthony. |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1141 |
Hi Anthony, you are right! This solution is not only faster it eliminates also the bug for negative numbers. Input:
num (dec):-5 lenght: 64bit 1111111111111111111111111111111111111111111111111111111111111011 Output: 1101111111111111111111111111111111111111111111111111111111111111 rnum (dec):-2305843009213693953 99 µs for one borev CFunction call 1101111111111111111111111111111111111111111111111111111111111111 rnum (dec):-2305843009213693953 17199 µs for the NEW bitreverse function call Regards Michael EDIT This seems a fast (12862/17199 µs) modification of Jims code to be: ' reverse bit order
' x = integer to reverse ' k = number of bits (8 or 16) Function brv(x,k) As integer Local n For n = 1 To k If x And (1<<(n-1)) Then brv=brv+(1<<(k-n)) EndIf Next End Function |
||||
Page 2 of 2 |
Print this page |