Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Build › Configuration › storeBits Public Function storeBits Doc Source Public Function std.Build.Configuration.storeBits
pub fn storeBits (comptime Int : type , buffer : []Int , bit_offset : usize , value : anytype ) void Params Int : type buffer : []Int bit_offset : usize value File Build/Configuration.zig: 3198
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222 pub fn storeBits (comptime Int : type , buffer : []Int , bit_offset : usize , value : anytype ) void {
const Value = @TypeOf (value );
const ValueInt = @Int (.unsigned , @bitSizeOf (Value ));
const value_int : ValueInt = @bitCast (value );
const index = bit_offset / @bitSizeOf (Int );
const small_bit_offset = bit_offset % @bitSizeOf (Int );
const available_bits = @bitSizeOf (Int ) - small_bit_offset ;
if (available_bits >= @bitSizeOf (ValueInt )) {
const shift = switch (native_endian ) {
.little => small_bit_offset ,
.big => available_bits - @bitSizeOf (ValueInt ),
};
buffer [index ] &= ~(((@as (Int , 1 ) << @intCast (@bitSizeOf (Value ))) - 1 ) << @intCast (shift ));
buffer [index ] |= @as (Int , value_int ) << @intCast (shift );
} else {
const DoubleInt = @Int (.unsigned , @bitSizeOf (Int ) * 2 );
const shift = switch (native_endian ) {
.little => small_bit_offset ,
.big => @bitSizeOf (DoubleInt ) - small_bit_offset - @bitSizeOf (ValueInt ),
};
const ptr : *align (@alignOf (Int )) DoubleInt = @ptrCast (buffer [index ..][0 ..2 ]);
ptr .* &= ~(((@as (DoubleInt , 1 ) << @intCast (@bitSizeOf (Value ))) - 1 ) << @intCast (shift ));
ptr .* |= @as (DoubleInt , value_int ) << @intCast (shift );
}
}
Store bits using native endianness when
valuespans multiple bytes. On big endian architectures:bit_offsetuses MSb 0 bit numbering. On little endian architectures:bit_offsetuses LSb 0 bit numbering.