简体   繁体   中英

Read bits from uint32_t

I want to use a function that returns a uint32_t which is supposed to contain 8 bits of information, in little-endian format. Could someone give me some C++ code on how to extract these bits from the uint32_t type into chars, booleans or into any other type that does not need the use of the Force to deal with! Because right now I do not have the patience to understand the whole concept of endianess. And the more I shallowly search the more complicated it seems...

PS. Although not what I am looking for, if someone could also post some code on how one could encode 8 bits (ex. 8 booleans) in an uint32_t would be interesting as I think it would help me understand the concept.

A sample in C, using union to force both an integer and combined bit values in the same address space, and with bitfields b0..b7 to hold single bit values:

#include <stdio.h>

union Bitfields {
    unsigned int as_int;
    struct {
        unsigned char b0:1,b1:1,b2:1,b3:1,
        b4:1,b5:1,b6:1,b7:1;
    } as_bit;
};

int main (void)
{
    Bitfields bitfield;

    bitfield.as_int = 73;

    printf ("%u %u %u\n", bitfield.as_int, bitfield.as_bit.b0, bitfield.as_bit.b1);

    return 0;
}

This allows easy read/write access to both the integer value and each one of your separate bits, whatever is more convenient.

Endianness should not be an issue here. Have you tried it?

Little endian means that the bytes that make up the uint32_t have the least significant byte stored at the lowest memory address and the most significant at the highest. Endian-ness should not enter the picture when extracting bits. It only enters the picture if you are doing things like accessing a uint32_t variable through a char *.

#include <iostream>
#include <cstdint>
using namespace std;

int main()
{
    unsigned char ch;
    uint32_t u32;
    bool b0, b1, b2, b3, b4, b5, b6, b7;

    // If the 8 bits you need are the least significant ones then getting
    // them is as easy as:
    u32 = 73;
    ch = u32 & 0xFF;
    cout << ch << endl;

    // To encode boolean variables is just as easy:
    b0 = b1 = b3 = b5 = 0;
    b2 = b4 = b6 = b7 = 1;
    u32 = (b0 << 0) | (b1 << 1) | (b2 << 2) | (b3 << 3) | (b4 << 4) |
        (b5 << 5) | (b6 << 6) | (b7 << 7);
    cout << hex << u32 << endl;

    return 0;
}

Endianness deals with memory loads/stores and not values

When accessing a variable, it will be loaded from memory using the default endianness of the system (little endian in the case of x86) and give you the correct value in the variable. Therefore if you want the 8 low bits, just apply a mask and get that value

x = someint32 & 0xff;

Likewise, assigning a char into an int or using it in an expression will just copy its values . The bit pattern will be sign-extended or zero-extended depending on the signness of the type, but you're still dealing with only values. Endianness is irrelevant here because there's no memory access

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM