如何从int到字节投,然后用bitshift运营商 [英] How to cast from int to byte, then use a bitshift operator
本文介绍了如何从int到字节投,然后用bitshift运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么下面不工作?我投一个int到字节,那么在位由7转向我看不出有任何问题出现。
不过,我得到错误信息precision ...需要的可能的损失:字节;实测值:INT
像素
是一个字节数组, C
是一个Color对象, ITER
是一个整数。
像素[ITER ++] =((字节)c.getRed())<< 7;
像素[ITER ++] =((字节)c.getGreen())≤;&下; 7;
像素[ITER ++] =((字节)c.getBlue())≤;&下; 7;
解决方案
在Java中,移位运算符返回 INT
价值,即使被转移的数量是字节
。你需要用中投为字节
围绕整个前pression:
像素[ITER ++] =(字节)(c.getRed()<< 7);
像素[ITER ++] =(字节)(c.getGreen()&所述; 7;);
像素[ITER ++] =(字节)(c.getBlue()&所述; 7;);
Why does the following not work? I cast an int to a byte, then shift the bits over by 7. I don't see any problems there.
However, I get the error message "possible loss of precision... required: byte; found: int"
pixels
is an array of bytes, c
is a Color object, iter
is an integer.
pixels[iter++] = ((byte) c.getRed()) << 7;
pixels[iter++] = ((byte) c.getGreen()) << 7;
pixels[iter++] = ((byte) c.getBlue()) << 7;
解决方案
In Java, the shift operators return an int
value, even if the quantity being shifted is a byte
. You need to wrap the cast to byte
around the entire expression:
pixels[iter++] = (byte) (c.getRed() << 7);
pixels[iter++] = (byte) (c.getGreen() << 7);
pixels[iter++] = (byte) (c.getBlue() << 7);
这篇关于如何从int到字节投,然后用bitshift运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文