当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Processing << (left shift)用法及代码示例


Processing, << (left shift)用法介绍。

用法

  • value << n

参数

  • value int:要移位的值
  • n int:左移的位数

说明

向左移动位。运算符左侧的数字将向右移动数字指定的位数。每次向左移动会使数字翻倍,因此每次向左移动都会将原始数字乘以 2。使用左移进行快速乘法或将一组数字打包成一个更大的数字。左移仅适用于自动转换为整数的整数或数字,例如 byte 和 char。

例子

int m = 1 << 3;   // In binary: 1 to 1000
println(m);  // Prints "8"
int n = 1 << 8;   // In binary: 1 to 100000000
println(n);  // Prints "256"
int o = 2 << 3;   // In binary: 10 to 10000
println(o);  // Prints "16"
int p = 13 << 1;  // In binary: 1101 to 11010
println(p);  // Prints "26"
// Packs four 8 bit numbers into one 32 bit number
int a = 255;  // Binary: 00000000000000000000000011111111
int r = 204;  // Binary: 00000000000000000000000011001100
int g = 204;  // Binary: 00000000000000000000000011001100
int b = 51;   // Binary: 00000000000000000000000000110011
a = a << 24;  // Binary: 11111111000000000000000000000000
r = r << 16;  // Binary: 00000000110011000000000000000000
g = g << 8;   // Binary: 00000000000000001100110000000000

// Equivalent to "color argb = color(r, g, b, a)" but faster
color argb = a | r | g | b;
fill(argb);
rect(30, 20, 55, 55);

有关的

相关用法


注:本文由纯净天空筛选整理自processing.org大神的英文原创作品 << (left shift)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。