Skip to content

Commit

Permalink
add example on bitwise operations on structure bitfields
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed May 12, 2024
1 parent ff02f43 commit 6b0a52d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/structure-bit-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ foo.a++; // will wrap around to 0 since this is unsigned
```

#source bitfield-wraparound.c

#source bitfield-bitwise-ops.c
18 changes: 18 additions & 0 deletions src/bitfield-bitwise-ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>

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);
}

0 comments on commit 6b0a52d

Please sign in to comment.