aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/bytes.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-06-26 13:06:47 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-06-26 13:06:47 -0400
commit8a4d5dc57b14e7c947c25970bb4d4f4ef91450f4 (patch)
treee36042760c7c22d5a08777939484c06830d73404 /src/stdlib/bytes.c
parentc2653404944dbd5a6f737877f0bad6fd1de018f1 (diff)
Add get_bit() method for Ints and Bytes
Diffstat (limited to 'src/stdlib/bytes.c')
-rw-r--r--src/stdlib/bytes.c8
1 files changed, 8 insertions, 0 deletions
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