From 8a4d5dc57b14e7c947c25970bb4d4f4ef91450f4 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 26 Jun 2025 13:06:47 -0400 Subject: Add get_bit() method for Ints and Bytes --- src/stdlib/bytes.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/stdlib/bytes.c') diff --git a/src/stdlib/bytes.c b/src/stdlib/bytes.c index b5c10aa2..48c8b93b 100644 --- a/src/stdlib/bytes.c +++ b/src/stdlib/bytes.c @@ -49,6 +49,14 @@ public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) { return text; } +public bool Byte$get_bit(Byte_t x, Int_t bit_index) { + if (Int$compare_value(bit_index, I(1)) < 0) + fail("Invalid bit index (expected 1 or higher): ", bit_index); + if (Int$compare_value(bit_index, I(8)) > 0) + fail("Bit index is too large! There are only 8 bits in a byte, but index is: ", bit_index); + return ((x & (Byte_t)(1L << (Int64$from_int(bit_index, true)-1L))) != 0); +} + #ifdef __TINYC__ #define __builtin_add_overflow(x, y, result) ({ *(result) = (x) + (y); false; }) #endif -- cgit v1.2.3