簡體   English   中英

從字節指針讀取位的差異

[英]Differences in reading bits from byte pointers

我發現很難理解以下代碼之間的區別:

    auto pixel = static_cast<uint32_t>( D3DCOLOR_RGBA( r, g, b, a ) );
    auto components = (uint8_t*)&pixel;
    std::array<uint8_t, 4> pixel_colours = { components[0], components[1], components[2], components[3] };

    uint8_t b_a = pixel >> 24;
    uint8_t b_r = ( pixel >> 16 ) & 0xFF;
    uint8_t b_g = ( pixel >> 8 ) & 0xFF;
    uint8_t b_b = pixel & 0xFF;

    return static_cast<uint32_t>( D3DCOLOR_RGBA( r, g, b, a ) );

對於r,g,b,a = {255、128、64、0},pixel_colours的值為{ 64, 128, 255, 0} 64、128、255、0 { 64, 128, 255, 0}而b_a,b_r,b_g,b_b的0, 255, 128, 64 0、255、128、64

我不明白為什么會發生這種差異-我希望它們是相同的。 有人可以解釋嗎?

components[0] ... components[1]以字節數組的形式訪問內存,並按照內存中的排列順序進行讀取。 pixel >> 24 ... pixel & 0xFF訪問int的邏輯值。 由於x86和x64(Intel)體系結構使用Little Endian,因此兩者有所不同。 有關Endianess的Wiki文章解釋了所有詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM