【发布时间】:2017-12-26 20:38:20
【问题描述】:
我正在阅读“Java The Complete Reference Ninth Edition”,作者给出了这个例子,用一个整数来创建一个 Color 对象,该整数对他所说的 RGB 值进行编码:
整数在第 16 到 23 位为红色,在第 23 位为绿色 8 到 15,位 0 到 7 为蓝色。以下是此构造函数的示例:
int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);
Color darkRed = new Color(newRed);
0xff000000 in hexadecimal is equivalent to 0b11111111000000000000000000000000 in binary
这是一个 32 位整数... 我理解按位操作,但我不明白的是:
数字开头的那些有什么用?为什么不以零开头
【问题讨论】:
-
我总是很好奇
Color#getRGB和Color(int, boolean)有什么问题......但我可能会遗漏一些东西:P
标签: java colors bit-manipulation rgb