diff --git a/mpf/core/utility_functions.py b/mpf/core/utility_functions.py index 74421ae1a..3e4d8fb89 100644 --- a/mpf/core/utility_functions.py +++ b/mpf/core/utility_functions.py @@ -802,11 +802,12 @@ def set_bit(hex_string, bit): bit (_type_): Bit to set, e.g. 3 Returns: - _type_: Returns the hex string with the bit set, e.g. '89' + _type_: Returns the hex string with the bit set, e.g. '89'. + The return string will be the same length as the input string. """ num = int(hex_string, 16) num |= 1 << bit - return Util.int_to_hex_string(num) + return Util.int_to_hex_string(num).zfill(len(hex_string)) @staticmethod def clear_bit(hex_string, bit): diff --git a/mpf/tests/test_Utility_Functions.py b/mpf/tests/test_Utility_Functions.py index 0039cde04..533cbd78b 100644 --- a/mpf/tests/test_Utility_Functions.py +++ b/mpf/tests/test_Utility_Functions.py @@ -215,3 +215,11 @@ def test_power_of_2(self): self.assertTrue(Util.is_power2(256)) self.assertFalse(Util.is_power2(3)) self.assertFalse(Util.is_power2(222)) + + def test_bit_functions(self): + self.assertEqual(Util.set_bit('00', 0), '01') + self.assertEqual(Util.set_bit('02', 2), '06') + self.assertEqual(Util.clear_bit('91', 4), '81') + self.assertEqual(Util.clear_bit('81', 3), '81') # no change + self.assertEqual(Util.check_bit('01', 0), True) + self.assertEqual(Util.check_bit('91', 5), False)