Shop OBEX P1 Docs P2 Docs Learn Events
Tachyon Boolean and Bit Manipulation — Parallax Forums

Tachyon Boolean and Bit Manipulation

dbpagedbpage Posts: 217
edited 2021-05-31 14:34 in Propeller 1

I am exploring Tachyon 5.7 on P1 for a commercial product.

How can I manipulate bits and set/reset bits based on boolean logical tests?

For example:
ShiftRight4longs(@Es) ' 128 bit shift register (no mask)
t := (Din<0) and (EAin>G2)
Es |= negx&t
PRI ShiftRight4longs(ptr)
long[ptr][3] >>= 1
long[ptr][3] |= long[ptr][2]<<31 long[ptr][2] >>= 1
long[ptr][2] |= long[ptr][1]<<31 long[ptr][1] >>= 1
long[ptr][1] |= long[ptr]<<31 long[ptr] >>= 1

where:
Din and EAin are values read in from i2c devices, G2 is a constant, E is a 128 bit shift register

Other methods count the number of bits set to 1 in a shift register as follows:
PRI Sum(ptr,n) | i,j
' Return the number of bits set to "1"
' ptr = address of first long
' n = number of longs
i := 0
j := 0
repeat n ' 1 to n longs
i := long[ptr]
i := i - ((i >> 1) & $55555555)
i := (i & $33333333) + ((i >> 2) & $33333333)
i := ((i + (i >> 4)) & $0F0F0F0F)
j += ((i * ($01010101))>>24)
ptr += 4
return j
PRI SumBits(n)
' Return the number of bits set to "1" in 1 long n
n := n - ((n >> 1) & $55555555)
n := (n & $33333333) + ((n >> 2) & $33333333)
n := ((n + (n >> 4)) & $0F0F0F0F)
return (n * ($01010101))>>24

Comments

  • Forgive me if I ask questions that have obvious answers!

    I found this http://www.forth.org/svfig/Len/bits.htm

    I see that the appropriate reserved words and symbols are in the Tachyon dictionary.

    The link resolves my not knowing how to leave the results of a boolean expression on the stack and gives these examples:
    Example 1:
    Leave a TRUE flag on the stack if A and B are unequal:
    It is permissible but inadvisable to use A B - if ... then ...

    Example 2:
    Leave a TRUE flag on the stack if A and B are unequal AND B and C are unequal
    You are in trouble if you use A B - C D - and

    For example 7 4 - 7 3 - and will result in false. In other words, two non-false arithmetic quantities and to false.

    This problem can be avoided if you always use 0<>.
    For example, A B - 0<> C D - 0<> and
    Even better here is A B <> C D <> and

    If I have any more questions, I'll ask!

Sign In or Register to comment.