Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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. Public Function

loadBits

Public Function
std.Build.Configuration.loadBits
pub fn loadBits(comptime Int: type, buffer: []const Int, bit_offset: usize, comptime Result: type) Result

Loads bits using native endianness when value spans multiple bytes. On big endian architectures, bit_offset uses MSb 0 bit numbering. On little endian architectures, bit_offset uses LSb 0 bit numbering. See storeBits.

Params

  • Int: type
  • buffer: []const Int
  • bit_offset: usize
  • Result: type

File

Build/Configuration.zig:3162
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
pub fn loadBits(comptime Int: type, buffer: []const Int, bit_offset: usize, comptime Result: type) Result {
    const index = bit_offset / @bitSizeOf(Int);
    const small_bit_offset = bit_offset % @bitSizeOf(Int);
    const ResultInt = @Int(.unsigned, @bitSizeOf(Result));
    switch (native_endian) {
        .little => {
            const result: ResultInt = @truncate(buffer[index] >> @intCast(small_bit_offset));
            const available_bits = @bitSizeOf(Int) - small_bit_offset;
            if (available_bits >= @bitSizeOf(ResultInt)) return @bitCast(result);
            const missing_bits = @bitSizeOf(ResultInt) - available_bits;
            const upper: ResultInt = @truncate(buffer[index + 1] & ((@as(usize, 1) << @intCast(missing_bits)) - 1));
            return @bitCast(result | (upper << @intCast(available_bits)));
        },
        .big => {
            const available_bits = @bitSizeOf(Int) - small_bit_offset;
            if (available_bits >= @bitSizeOf(ResultInt)) {
                const shift = available_bits - @bitSizeOf(ResultInt);
                const result: ResultInt = @truncate(buffer[index] >> @intCast(shift));
                return @bitCast(result);
            }
            const mask = (@as(Int, 1) << @intCast(available_bits)) - 1;
            const result: ResultInt = @intCast(buffer[index] & mask);
            const missing_bits = @bitSizeOf(ResultInt) - available_bits;
            const lower: ResultInt = @truncate(buffer[index + 1] >> @intCast(@bitSizeOf(Int) - missing_bits));
            return @bitCast((result << @intCast(missing_bits)) | lower);
        },
    }
}