pyftdi icon indicating copy to clipboard operation
pyftdi copied to clipboard

BitSequence comparison issue

Open addinh opened this issue 4 years ago • 1 comments

It seems the comparison functions for BitSequence may be erroneous? There are nots in the functions for <=, <, >, >= that shouldn't be there, unless I'm understanding the purpose of comparison wrong

https://github.com/eblot/pyftdi/blob/2ad8a1ab77ac7234e662fb4869c59737e755e29f/pyftdi/bits.py#L234-L250

Adding to this, the base function _cmp(self, other) also seems odd. I'm assuming the comparison is based on int value, but the function compares length first (which would fail in an example case: 101 vs 0010), then compares bit by bit. However, n from enumerate should only have values from 0 to len(self)-1. This makes the function less than always return True if both BitSequences have the same length.

https://github.com/eblot/pyftdi/blob/2ad8a1ab77ac7234e662fb4869c59737e755e29f/pyftdi/bits.py#L252-L260

A simplier way to compare would be to convert to int and compare directly, for example int(self) and int(other). Again, I may be understanding the purpose wrong, but if comparison deals with the int values of BitSequences then using the conversion function directly should make it simple.

addinh avatar Jun 18 '21 09:06 addinh

I think you are very right. This code has not been maintained for over 10 years, and is not part of the CI/CD.

Patches are very welcome - there are even better if you can include some tests.

eblot avatar Jun 18 '21 09:06 eblot