Weird bit-twiddling coercion behavior
ziggit.dev·5d
Ditherpunk
Preview
Report Post

January 16, 2026, 7:23pm 1

I’m slowly going insane as to whether or not this is a legit bug or if I’m just holding things wrong.

Consider the following C-code:

#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv) {
uint8_t a = 0x30;
uint16_t b = 0xCAFE;
a = (a & 0xF0) | (b & 0x0F);
printf("%02X\n", a);
return 0;
}

Which will correctly print out 3E, whereas the equivalent Zig-version:

const std = @import("std");

pub fn main() void {
var a: u8 = 0x30;
const b: u16 = 0xCAFE;
a = (a & 0xF0) | (b & 0x0F);
std.debug.print("{X}\n", .{a});
}

…makes the compiler choke and die over the fact that b is a u16. Which kinda makes sense if you consider bit twiddling to be like any other integer arithmetic. But here I mask off the least significant nib…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help