From 6b0a52dd9b96e10f1796e931c752eb3bcf193339 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Sun, 12 May 2024 22:50:55 +0200 Subject: [PATCH] add example on bitwise operations on structure bitfields --- modules/structure-bit-fields.md | 2 ++ src/bitfield-bitwise-ops.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/bitfield-bitwise-ops.c diff --git a/modules/structure-bit-fields.md b/modules/structure-bit-fields.md index c5a10c63..d9d85ab1 100644 --- a/modules/structure-bit-fields.md +++ b/modules/structure-bit-fields.md @@ -41,3 +41,5 @@ foo.a++; // will wrap around to 0 since this is unsigned ``` #source bitfield-wraparound.c + +#source bitfield-bitwise-ops.c diff --git a/src/bitfield-bitwise-ops.c b/src/bitfield-bitwise-ops.c new file mode 100644 index 00000000..ba3b6d2e --- /dev/null +++ b/src/bitfield-bitwise-ops.c @@ -0,0 +1,18 @@ +#include + +struct foo_s { + unsigned int a : 3; + unsigned int b : 5; +} foo; + +int +main(void) +{ + foo.a = 5; /* 101 */ + foo.b = 16; /* 10000 */ + + foo.a = foo.a ^ foo.b; + unsigned int xor = foo.a ^ foo.b; + + printf("%u vs %u\n", foo.a, xor); +}