Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:06 01 Jul 2025 Privacy Policy
Jump to

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 : Bit manipulation

Author Message
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 01:29pm 03 Apr 2016
Copy link to clipboard 
Print this post

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 Zealand
Posts: 2428
Posted: 04:32pm 03 Apr 2016
Copy link to clipboard 
Print this post

try this:

Do
Input A%
B% = ((A% * &h202020202) And &h10884422010) Mod 1023
Print Bin$(A%, 8), Bin$(B%, 8)
Print B%
Print
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 :-)Edited by robert.rozee 2016-04-05
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 07:21pm 03 Apr 2016
Copy link to clipboard 
Print this post

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: Germany
Posts: 1566
Posted: 03:11am 04 Apr 2016
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4032
Posted: 04:41am 04 Apr 2016
Copy link to clipboard 
Print this post

  robert.rozee said   try this:

Do
Input A%
B% = ((A% * &h202020202) And &h10884422010) Mod 1023
Print Bin$(A%, 8), Bin$(B%, 8)
Print B%
Print
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 :-)


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: Australia
Posts: 3281
Posted: 05:13am 04 Apr 2016
Copy link to clipboard 
Print this post

  JohnS said  It needs 64-bit, which MMBasic does not have I gather.

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: Germany
Posts: 1566
Posted: 05:27am 04 Apr 2016
Copy link to clipboard 
Print this post

  robert.rozee said   try this:


@rob: nice find! (for byte length)

Michael
causality ≠ correlation ≠ coincidence
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4032
Posted: 06:01am 04 Apr 2016
Copy link to clipboard 
Print this post

  Geoffg said  
  JohnS said  It needs 64-bit, which MMBasic does not have I gather.

Yes it does, and what's more it is faster than floating point.

I stand corrected, thanks.

I sure hope it is indeed faster than floating point!!

JohnEdited by JohnS 2016-04-05
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1566
Posted: 10:46am 04 Apr 2016
Copy link to clipboard 
Print this post

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
Edited by twofingers 2016-04-05
causality ≠ correlation ≠ coincidence
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025