Adding bits in variables
Moskog
Posts: 554
Hello,
Now I have this little problem, it is a part of a simple validation routine in a transmitter-reciever environment! I want the transmitter to send a number (checksum) along with·one or·more
variables. The checksum is all the bits in all the variables added together,
like this:· something = 13······binary 1101······ checksum = 3.
This is an example on how to find the checksum of the variable Something:
something VAR Nib
checksum VAR Nib
checksum = 0
checksum = something.BIT0 + something.BIT1 + something.BIT2 + something.BIT3
But this is not a good programming-solution if I have·word-sized variables or even several of them.
Being looking after a better way but can't seem to find anything in my books on adding bits in variables.
Anyone there with a good idea?
KjellO
Now I have this little problem, it is a part of a simple validation routine in a transmitter-reciever environment! I want the transmitter to send a number (checksum) along with·one or·more
variables. The checksum is all the bits in all the variables added together,
like this:· something = 13······binary 1101······ checksum = 3.
This is an example on how to find the checksum of the variable Something:
something VAR Nib
checksum VAR Nib
checksum = 0
checksum = something.BIT0 + something.BIT1 + something.BIT2 + something.BIT3
But this is not a good programming-solution if I have·word-sized variables or even several of them.
Being looking after a better way but can't seem to find anything in my books on adding bits in variables.
Anyone there with a good idea?
KjellO
Comments
something var word
count var byte
index var byte
count = 0
for index=0 to 15
· count = count + something.0
· something = something /2· 'or use shiftright by 1 position
next
regards peter
Guess I can't say "something.index" either, would probably make same error message. (I'm using PBasic 2.5)
KjellO
Thank you so much and have a nice weekend!
KjellO
Which can be simplified somewhat using methods shown in Chapter 5 of hacker's delight which is all on the subject of counting bits, including tricks for larger arrays.
Another method is unique to the PBASIC interpreter, using the DCD and NCD operators. The execution time of this is proportional to the number of ones in the word:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
KjellO