java - 从 RGB 图像中分离出 RGB channel 时 &0xff 的意义是什么?

标签 java image-processing

我有以下代码片段

for(int row=0; row<r; row++)
{
    for(int col=0; col<c; col++)
    {
        alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
        redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
        greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
        bluePixels[row][col] = (RGBarray[ii]&0xff);
        ii++;
    }
}

为什么要在移位运算后使用按位与运算& 0xff

最佳答案

Why do we have to use the bitwise AND operation ' & 0xff' after the shifting operation?

Oxff 是十六进制数,等于十进制数 255。

在您的情况下,& 0xff 确保所有像素值范围都在 0 到 255 之间(即正 8 位)。例如,如果任何值大于 255,它将在 0-255 范围内截断

    int value=257;
    int result = value & 0xff; // result will be 1

因此,它的工作原理类似于正值的余数运算符 %。但按位运算符 & 比取余运算符 % 更快。

    int result = value % 256; //You will get same result for positive value 

关于java - 从 RGB 图像中分离出 RGB channel 时 &0xff 的意义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742777/

相关文章:

Java NIO2 并发完成处理程序

java - 替换已弃用的 org.apache.http.conn.scheme.SchemeRegistry、ThreadSafeClientConnManager 类等

java - 为什么 java 在解析这个 XML 时会保存空字符串?

Java 流 : get values grouped by inner map key

python - 在 python 中将图像划分为 5x5 block 并计算每个 block 的直方图

c++ - OpenCV 错误 : Assertion failed ((img. depth() == CV_8U || img.depth() == CV_32F)

ios - 只使用 CLImageEditor 中的一个工具

java - 构建 Android Bundle 时出现问题 - 引发错误 "File ' root/lib/commons-io-2.4.jar' 使用保留文件或目录名称 'lib' "

python - OpenCV 大图像(大于 1gb)替代方案

c++ - opencv 中的(自适应)阈值错误(cvarrToMat 中的错误参数(未知数组类型))