如何用 bit_permute_step 代替手动位操作
我怎样才能用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,即:
掩码
0x000000ff
表示的v
的位包含r
分量,bit_permute_step
将交换它们相应的位 16(距离参数)位于左侧,对应于v
的b
组件。因此,bit_permute_step(v, 0x000000ff, 16)
会将r
与b
交换,从而将 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 代码)可以是:Yes, namely:
The bits of
v
indicated by the mask0x000000ff
contain ther
component,bit_permute_step
will exchange them with the corresponding bits 16 (the distance parameter) places to the left, which corresponds to theb
component ofv
. Sobit_permute_step(v, 0x000000ff, 16)
will swap ther
with theb
, 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: