Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draw-line challenge bug #297

Open
msm1089 opened this issue Jul 10, 2021 · 1 comment
Open

draw-line challenge bug #297

msm1089 opened this issue Jul 10, 2021 · 1 comment

Comments

@msm1089
Copy link

msm1089 commented Jul 10, 2021

Using the drawline method provided as the solution, it does not handle cases where start_byte != end_byte properly. In end_byte, only the LSB is set.

E.g.

S = BitsScreen()
screen = []
for _ in range(20):
    screen.append(int('00000000', base=2))
S.draw_line(screen, 32, 68, 92)
for row in screen:
    print(f"{row: 09b}")

results in

 00000000
 00000000
 00000000
 00000000
 00000000
 00000000
 00000000
 00000000
 00001111
 11111111
 11111111
 00001000 <---only bit corresponding to x2 = 92 is set
 00000000
 00000000
 00000000
 00000000
 00000000
 00000000
 00000000
 00000000

I have tried to come up with a solution for this but could not. Could anyone provide the correct solution perhaps?

@msm1089
Copy link
Author

msm1089 commented Jul 10, 2021

UPDATE

In the end I solved it by changing the last else block:

    else:
        start_mask = (1 << (8 - start_bit)) - 1
        end_mask = ~((1 << (8 - end_bit - 1)) - 1)
        screen[start_byte] |= start_mask
        screen[end_byte] = int('11111111', base=2)
        screen[end_byte] &= end_mask

I looked back at the first challenge (bits) in bit_manipulation series. I set the last bit to all 1s, then used the method from the clear_bits_index_to_lsb method in the bits challenge.

I have a couple of questions about this.

  1. Setting all bits in a byte is done using int('11111111', base=2). This could just be done by setting it to equal 255. What is the reason to use the much longer way?
  2. Is there some mask that can be used to set all bits from MSB to an index? This would prevent the need to first set all bits then clear the bits to the right of the index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants