#๐Ÿ”’ count set bits in succession

43 messages ยท Page 1 of 1 (latest)

violet ice
#

does anyone know how i can count the number of bits in succession in an 8 bit number?

for example, there are lines of 3 bits and lines of 2 bits:

0b 1110 0011

something with binary instructions would be really helpful. thank you

candid yewBOT
#

@violet ice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

violet ice
#

would a lookup table work?

solar snow
crystal jewel
#

I would rather calculate on demand

#

But, it's possible

violet ice
#

that's what i would say

#

calculate on the fly would be better

solar snow
#

but also, int.bit_count exists, and you can extract selected bits with &, so

(x & 0b1111).bit_count()```
gets the amount of lower 4 bits set
violet ice
#

how many bits are set IN SUCCESSION

#

so in a "line"

crystal jewel
violet ice
#

0b11100110 has 5 bits set, but has 1 line of 3 and 1 line of 2

crystal jewel
#

Either happen -> 3

violet ice
solar snow
#

ah so the spaces in your original example are just for readability

violet ice
#

sorry if i didn't mention that in the message

crystal jewel
#

Even easier

#

Is that a homework?

violet ice
#

personal project

solar snow
#

if you have these tools in your toolbox:

  • you know how to get some limit for the amount of bits (you do, it is at most 8)
  • you know how to loop and get each bit
  • you know how to keep track of state across loop iterations
    then you have everything you need to create an algorithm that deals with spans of set bits
olive rapids
#

num&1 to see current last bit
num>>1 to move the bits right 1 space (thus removing last bit, and second last becomes new last). Save the result of that to use

You'd want to repeat those 2 operations in a loop - until the num becomes 0 (no more bits set to check) and manually keep count of groups

That's the low-memory way.

Memory-intensive way would be to treat it as sequence of characters (so each bit becomes one byte...) and throw it into something like intertools.groupby

violet ice
#

makes sense

#

i'm using this to score connect 4 bitboard positions

#

i was lead to this SO post

#

didn't really help since i had zero idea what they meant

crystal jewel
#

Do you want me to show the solution to you?

violet ice
#

that works shrug

crystal jewel
#
def solver(v: int) -> int:
    max_c = c = 0
    while v:
        if v & 1:
            c+=1
        else:
            if max_c < c:
                max_c = c
            c = 0
        v >>= 1
    return c
#

You could do v %= 256 or v &= 255 to make sure it only work with 8 bit number tho (the last 8 bit number if exceed)

cedar widget
#

was groupby suggested?

>>> s = '11100011'
>>> from itertools import groupby
>>> for k, g in groupby(s):
...     print(k, len(list(g)))
...
1 3
0 3
1 2
crystal jewel
#

Yes

#

Memory-intensive way would be to treat it as sequence of characters (so each bit becomes one byte...) and throw it into something like intertools.groupby

violet ice
candid yewBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.