简体   繁体   English

从 uint32_t 读取位

[英]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.我想使用一个返回uint32_t的函数,该函数应该包含 8 位信息,采用 little-endian 格式。 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!有人可以给我一些关于如何将这些位从uint32_t类型提取为字符、布尔值或任何其他不需要使用 Force 来处理的类型的 C++ 代码! 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.虽然不是我想要的,但如果有人也可以发布一些关于如何在uint32_t编码 8 位(例如 8 个布尔值)的代码会很有趣,因为我认为这会帮助我理解这个概念。

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: C 中的一个示例,使用union强制在同一地址空间中同时使用整数和组合位值,并使用位域b0..b7来保存单个位值:

#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.小端意味着组成 uint32_t 的字节的最低有效字节存储在最低内存地址,最高有效字节存储在最高地址。 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 *.如果您正在执行诸如通过 char * 访问 uint32_t 变量之类的操作,它只会进入图片。

#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.访问变量时,它将使用系统的默认字节序(x86 中的小字节序)从内存中加载,并为您提供变量中的正确 Therefore if you want the 8 low bits, just apply a mask and get that value因此,如果您想要 8 个低位,只需应用一个掩码并获得该值

x = someint32 & 0xff;

Likewise, assigning a char into an int or using it in an expression will just copy its values .同样,将char分配给int或在表达式中使用它只会复制其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字节序在这里无关紧要,因为没有内存访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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