Counting bits


Closed Thread
Results 1 to 4 of 4

Thread: Counting bits

  1. #1
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263

    Default Counting bits

    Does anyone know a quick and easy (or sneaky) way to count bits?

    I want to look at either the top or bottom 4 bits of a word and determine whether an odd or even number of bits are high (or low; doesn't matter).

    Right now, I'm using LOOKDOWN and it works just fine; but I keep thinking there's a more elegant way to do it.
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Russ,

    Not particularly "Sneaky" ... but it's "Quick".
    Code:
    Counter  VAR BYTE BANK0
    ODD      VAR Counter.0
    TestWord VAR WORD BANK0
    
    ASM
        clrf   _Counter
        btfsc  _TestWord,0
        incf   _Counter, F
        btfsc  _TestWord,1
        incf   _Counter, F
        btfsc  _TestWord,2
        incf   _Counter, F
        btfsc  _TestWord,3
        incf   _Counter, F
    ENDASM
    
    If ODD THEN
      ; ODD number of bits in the TestWord<3:0>
    ENDIF
    Last edited by Darrel Taylor; - 10th October 2008 at 08:57. Reason: Changed Count to Counter
    DT

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi, Russ

    first, shift right your word 12 times if > 4095 ...

    mask the result to keep the 4 lower bits,

    then 6 possibilities remain for an even count of the bits ...

    ( call the result "ABCD" ... if even, ABCD = Ch,Ah,9h,6h,5h or 3h )

    A SELECT CASE will do it.

    mask your word to keep the 4 lower bits

    Then 6 possibilities ...

    The SELECT CASE ...

    it's one way ...in PbP !!! ( lol )


    or use "NCD" function in a loop ... which looks furiously to what Darrel Wrote !!!


    Alain
    Last edited by Acetronics2; - 10th October 2008 at 09:07.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    Thanks to both of you.

    I'm emulating a 16-bit twisted ring counter--actually a very twisted counter. If an even number of bits in the top four are high, the whole works is shifted left one place and the lowest bit is set to 1; if an odd number of bits are high, again a one-place shift left, but the default 0 shifted in is retained.

    Just now, the target device is a 16F87xA.
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  3. PIC's ports individual bits manipulation - how to?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 38
    Last Post: - 27th February 2007, 22:34
  4. Need clever way to convert 10 bits to 8 bits
    By MikeTamu in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 2nd September 2005, 15:13
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts