Shop OBEX P1 Docs P2 Docs Learn Events
Char((value <-= 1) & 1 + "0") "Question" — Parallax Forums

Char((value <-= 1) & 1 + "0") "Question"

John MichaelJohn Michael Posts: 37
edited 2011-01-05 12:57 in Propeller 1
Newbe .... Would someone be kind enough to explain how the following code reads so I can make sure I understand it right.
____________________________________________________________________

PUB Bin(value, digits)

{{Send value as binary characters up to digits in length.
Parameters:
value - byte, word, or long value to send as binary characters.
digits - number of binary digits to send. Will be zero padded if necessary.}}

value <<= 32 - digits
repeat digits
Char((value <-= 1) & 1 + "0")

____________________________________________________________________________________

Thanks,
John Michael

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-05 12:57
    I'll just explain the more complicated parts. Look in the Propeller Manual for a description of the various operators and statements.

    value <<= 32 - digits

    This shifts value to the left (<<=) so that the first bit to be output is in bit 31 (the most significant bit of value).

    (value <-= 1) & 1 + "0"

    For each bit to be output, this rotates value left (<-=) by one bit position so the most significant bit (bit 31) becomes the least significant bit (bit 0) and all the other bits follow. It then masks off (&) the least significant bit, so you have just a zero or one, then it adds (+) the character "0" so you have either "0" or "1" for zero or one respectively.

    This character is sent to the output (Char) and the whole thing is repeated once for each binary digit specified (repeat digits).

    Try doing the whole thing on a piece of paper step by step with some sample numbers.
Sign In or Register to comment.