summaryrefslogtreecommitdiffhomepage
path: root/examples/bits2.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-05-22 20:45:31 +0200
committerTyge Løvset <[email protected]>2022-05-22 20:45:31 +0200
commit314a41be6b39b6c5967d79555dbf018dbc7eb5f6 (patch)
tree881e2e6654a91357dfc225c9e67a80bfca37263c /examples/bits2.c
parentc0de7d59a09417e2e18909f808258e909c36ee09 (diff)
downloadSTC-modified-314a41be6b39b6c5967d79555dbf018dbc7eb5f6.tar.gz
STC-modified-314a41be6b39b6c5967d79555dbf018dbc7eb5f6.zip
Rewrote cbits to make it dual: fixed-sized or dynamically sized by adding optional i_len template parameter. Renamed cbits_set_values() to cbits_set_pattern(). Added example bits2.c
Diffstat (limited to 'examples/bits2.c')
-rw-r--r--examples/bits2.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/bits2.c b/examples/bits2.c
new file mode 100644
index 00000000..e9202efc
--- /dev/null
+++ b/examples/bits2.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+// Example of static sized (stack allocated) bitsets
+
+#define i_type Bits
+#define i_len 80 // enable fixed bitset on the stack
+#include <stc/cbits.h>
+
+int main()
+{
+ Bits s1 = Bits_from("1110100110111");
+
+ printf("size %" PRIuMAX "\n", Bits_size(s1));
+ char buf[256];
+ Bits_to_str(s1, buf, 0, -1);
+ printf("buf: %s: count=%" PRIuMAX "\n", buf, Bits_count(s1));
+
+ Bits_reset(&s1, 8);
+ c_autobuf (str, char, Bits_size(s1) + 1)
+ printf(" s1: %s\n", Bits_to_str(s1, str, 0, -1));
+
+ Bits s2 = Bits_clone(s1);
+
+ Bits_flip_all(&s2);
+ Bits_reset(&s2, 66);
+ Bits_reset(&s2, 67);
+ printf(" s2: ");
+ c_forrange (i, Bits_size(s2))
+ printf("%d", Bits_test(s2, i));
+ puts("");
+
+ printf("xor: ");
+ Bits_xor(&s1, s2);
+ c_forrange (i, Bits_size(s1))
+ printf("%d", Bits_test(s1, i));
+ puts("");
+
+ printf("all: ");
+ Bits_set_pattern(&s1, 0x3333333333333333);
+ c_forrange (i, Bits_size(s1))
+ printf("%d", Bits_test(s1, i));
+ puts("");
+}