Shop OBEX P1 Docs P2 Docs Learn Events
Adding bits in variables — Parallax Forums

Adding bits in variables

MoskogMoskog Posts: 554
edited 2008-07-19 17:25 in BASIC Stamp
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

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-07-19 16:20
    Use a for loop to divide something by 2, before division add bit0 to a counter.

    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
  • MoskogMoskog Posts: 554
    edited 2008-07-19 16:43
    Well.. I guess Count was a bad name of a variable here, but replacing it with a different name ended up with the error message Expected a variable modifier on "something.0".
    Guess I can't say "something.index" either, would probably make same error message. (I'm using PBasic 2.5)

    KjellO
  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-19 16:53
    Use "something.bit0"
  • MoskogMoskog Posts: 554
    edited 2008-07-19 17:00
    Yes, now it works, but Something disappeard into zero, I have to remember to save it before prosessing!

    Thank you so much and have a nice weekend!

    KjellO
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-07-19 17:03
    x = something  ' a word variable
    x = (x & $5555) + (x>>1 & $5555)
    x = (x & $3333) + (x>>2 & $3333)
    x = (x & $0f0f) + (x>>4 & $0f0f)
    x = (x & $00ff) + (x>>8 & $00ff)
    ' x is now = number of ones in somthing
    



    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:

    x = something
    cs = 0
    DO while x
      x = x - DCD (NCD x -1)
      cs = cs + 1
    LOOP
    ' cs is a count of the number of ones in something
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2008-07-19 17:25
    Yess! I'm on my knees in the dust, thats an awesome solution! Thank's so much!

    KjellO
Sign In or Register to comment.