如何用 bit_permute_step 代替手动位操作

发布于 2025-01-13 21:09:59 字数 705 浏览 2 评论 0原文

我怎样才能用 http://programming.sirrida.de/perm_fn 替换以下手动逻辑。 html#bit_permute_step

 unsigned int ConvertRGBAToBGRA(unsigned int v) {
    unsigned char r = (v)& 0xFF;
    unsigned char g = (v >> 8) & 0xFF;
    unsigned char b = (v >> 16) & 0xFF;
    unsigned char a = (v >> 24) & 0xFF;
    return (a << 24) | (r << 16) | (g << 8) | b;
};

有没有更好的方法使用 http://programming.sirrida.de/perm_fn 来执行上述操作。 html#bit_permute_step

How can I do replace the following manual logic by http://programming.sirrida.de/perm_fn.html#bit_permute_step ?

 unsigned int ConvertRGBAToBGRA(unsigned int v) {
    unsigned char r = (v)& 0xFF;
    unsigned char g = (v >> 8) & 0xFF;
    unsigned char b = (v >> 16) & 0xFF;
    unsigned char a = (v >> 24) & 0xFF;
    return (a << 24) | (r << 16) | (g << 8) | b;
};

Is there a better way to do the above using http://programming.sirrida.de/perm_fn.html#bit_permute_step ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2025-01-20 21:09:59

是的,即:

return bit_permute_step(v, 0x000000ff, 16);

掩码0x000000ff表示的v的位包含r分量,bit_permute_step将交换它们相应的位 16(距离参数)位于左侧,对应于 vb 组件。因此,bit_permute_step(v, 0x000000ff, 16) 会将 rb 交换,从而将 RGBA 转换为 BGRA(也将 BGRA 转换为 RGBA) ,因为交换是其自身的逆)。

这也可以通过排列计算器找到:http://programming.sirrida.de/calcperm.php< /a>

使用索引 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31(源索引)并禁用位组移动。


32 位整数的 bit_permute_step 的 C++ 实现(也可用作 C 代码)可以是:

uint32_t bit_permute_step(uint32_t x, uint32_t m, uint32_t shift) {
    uint32_t t;
    t = ((x >> shift) ^ x) & m;
    x = (x ^ t) ^ (t << shift);
    return x;
}

Yes, namely:

return bit_permute_step(v, 0x000000ff, 16);

The bits of v indicated by the mask 0x000000ff contain the r component, bit_permute_step will exchange them with the corresponding bits 16 (the distance parameter) places to the left, which corresponds to the b component of v. So bit_permute_step(v, 0x000000ff, 16) will swap the r with the b, and hence turn RGBA into BGRA (and also BGRA into RGBA, because a swap is its own inverse).

This could also be found via the permutation calculator: http://programming.sirrida.de/calcperm.php

Use the indices 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 (source indices) and disable bit-group moving.


A C++ implementation (also usable as C code) of bit_permute_step for 32-bit integers could be:

uint32_t bit_permute_step(uint32_t x, uint32_t m, uint32_t shift) {
    uint32_t t;
    t = ((x >> shift) ^ x) & m;
    x = (x ^ t) ^ (t << shift);
    return x;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文