Internet PLC Forum
General => Technical support => Topic started by: Lorne Van Dusen on May 25, 2017, 06:07:57 PM
-
I did a search on the Internet for how to reverse bits and was surprised to find out how many times people have asked that question however most answers were for a C or C++
is there a simple way to reverse the bits in either a 8 bit or 16 bit value stored say in DM1 in TBASIC Fx series plc
example dm1 = 11100000 the result should be 00000111
-
Below is a 16 bit reverse bit example.
A = &HF5A0 ' put any value you want in A
B = 0
FOR I = 0 to 15
IF TESTBIT(A, 15-I)
SETBIT B, I
ELSE
CLRBIT B, I
ENDIF
NEXT
' Result: B = &H05AF if A=&HF5A0
-
You can eliminate the "else" clause in the "if" statement as all of the bits in B have been set to 0 outside the for loop:
B = 0
FOR I = 0 to 15
IF TESTBIT(A, 15-I)
SETBIT B, I
ENDIF
NEXT
Gary D