![]() |
Forum Index : Microcontroller and PC projects : Bit manipulation
Author | Message | ||||
jman![]() Guru ![]() Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi Can one you smart people suggest a method of bit manipulation to do the following Before After 10100011 11000101 00110000 00001100 ETC.. Regards Jman |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2428 |
try this: Do
Input A% B% = ((A% * &h202020202) And &h10884422010) Mod 1023 Print Bin$(A%, 8), Bin$(B%, 8) Print B% Loop see: https://corner.squareup.com/2013/07/reversing-bits-on-arm.html note that the above algorithm seems to be the slowest of those listed in the above link, but then in terms of the available datatypes provided by mmbasic, it may well be competitive. and you must admit, it is cool! cheers, rob :-) |
||||
jman![]() Guru ![]() Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi Rob Cheers for the info the above works a treat and yes it's rather cool Regards Jman |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1566 |
Hi Jman, are you looking for the good old bit order reversal ? ' borev(integer_num, bit_lenght)
' "integer_num" can be any MMBasic integer. ' "bit_lenght" can be any integer in the range from 1 to 64. ' returns a unsigned integer of "integer_num" with reversed bit order CFunction borev 00000000 8CA90004 1D200005 8CAC0000 5520002C 00002021 5180002A 00002021 8C8B0000 8C8D0004 2587FFFF 00002021 00002821 24020001 240A0001 2443FFFF 30680020 006A1804 00603021 0008300A 0008180B 006B1824 00CD3024 00664025 1100000D 24420001 30E80020 00EA1804 00603021 0008300A 0008180B 00831821 00A63021 0064202B 00862021 00803021 00602021 00C02821 00021FC3 0123302A 14C00009 24E7FFFF 1469FFE5 2443FFFF 0182402B 1100FFE3 30680020 10000003 00801021 00002821 00801021 03E00008 00A01821 End CFunction Regards Michael causality ≠ correlation ≠ coincidence |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4032 |
It needs 64-bit, which MMBasic does not have I gather. I wonder for which values it fails... John |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3281 |
Yes it does, and what's more it is faster than floating point. Geoff Graham - http://geoffg.net |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1566 |
|
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4032 |
I stand corrected, thanks. I sure hope it is indeed faster than floating point!! John |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1566 |
BTW: The CFunction is about 20% faster than the pure Basic version Test code (MX170/MMBasic 5.1): Timer=0
A%=&HF0 For i = 1 To 10000 B%=borev(A%,8) Next i Print Timer-287 '287ms time to complete 10000 loops Print Bin$(A%, 8), Bin$(B%, 8) End '1138ms/10000 pure Basic { B% = ((A% * &h202020202) And &h10884422010) Mod 1023} ' 949ms/10000 CFunction borev CFunction borev 00000000 8CA90004 1D200005 8CAC0000 5520002C 00002021 5180002A 00002021 8C8B0000 8C8D0004 2587FFFF 00002021 00002821 24020001 240A0001 2443FFFF 30680020 006A1804 00603021 0008300A 0008180B 006B1824 00CD3024 00664025 1100000D 24420001 30E80020 00EA1804 00603021 0008300A 0008180B 00831821 00A63021 0064202B 00862021 00803021 00602021 00C02821 00021FC3 0123302A 14C00009 24E7FFFF 1469FFE5 2443FFFF 0182402B 1100FFE3 30680020 10000003 00801021 00002821 00801021 03E00008 00A01821 End CFunction causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |