Char((value <-= 1) & 1 + "0") "Question"
John Michael
Posts: 37
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
____________________________________________________________________
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
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.