impala位运算函数最全版

x33g5p2x  于2021-12-25 转载在 其他  
字(1.6k)|赞(0)|评价(0)|浏览(922)

本文基于impala3.2版本,所有的内置位运算函数;

一、位运算函数
序号语法类型/方法名称输出类型使用说明
1bitand (integer_type a,same_type b)和输入类型一致按位取与运算&
2bitnot (integer_type a)和输入类型一致按位取非运算~
3bitor (integer_type a,same_type b)和输入类型一致按位取或运算|
4bitxor (integer_type a,same_type b)和输入类型一致按位取异或运算^
5countset (integer_type a)和输入类型一致返回1的个数(默认)
6countset (integer_type a , int zero_or_one)和输入类型一致返回1或0的个数
7getbit (integer_type a , int postition )和输入类型一致获取指定位置的数字
8rotateleft (integer_type a , int postitions)和输入类型一致循环移位(往左)
9rotateright (integer_type a , int postitions)和输入类型一致循环移位(往右)
10setbit (integer_type a , int postition)和输入类型一致设置指定位置为1(默认)
11setbit (integer_type a , int postition , int zero_or_one )和输入类型一致设置指定位置为1或0
12shiftleft (integer_type a , int postition)和输入类型一致逻辑移位(往左)
13shiftright (integer_type a , int postition)和输入类型一致逻辑移位(往右)

示例如下:

①位运算函数对所有整型数据起作用:int , bigint , smallint , tinyint ;

②位运算函数中任何参数为null,则返回值为null;

③所有impala整型都是带符号的;

  1. 序号| 计算示例 | 计算过程 |输出结果
  2. 1 | bitand(12, 5) 00001100 & 00000101 400000100 --相同位置都为1的时候才会为1
  3. 2 | bitnot(16) ~00010000 -1711101111)--0110
  4. 3 | bitor(1, 4) 00000001 | 00000100 500000101 --只要有一个是1,就是1
  5. 4 | bitxor(0, 15) 00000000 ^ 00001111 1500001111 --相同的位置为0,不相同的为1
  6. 5 | countset(17) 00010001 2 --返回整值的1的个数
  7. 6 | countset(17,0) 00010001 6 --返回整值的指定参数的个数(这里返回的0
  8. 7 | getbit(1, 0) 00000001 1 --返回指定位置是0还是1(这里从右往左从0开始第一位是1
  9. 8 | rotateleft(-127, 3) 10000001 -> 00001100 16 --这里是把整数位整体往左指定位(这个例子里面是3
  10. 9 | rotateright(-127, 3)10000001 -> 00110000 48 --这里是把整数位整体往右指定位(这个例子里面是3
  11. 10 | setbit(0, 3) 00000000 -> 00001000 8 --把整型指定位数变成1(从右往左从0开始)
  12. 11 | setbit(7, 2, 0) 00000111 -> 00000011 3 --把整型指定位数变成第三个参数(从右往左从0开始)
  13. 12 | shiftleft(127, 5) 01111111 -> 11100000 -32 --把整型往左移动指定的位数(超出的舍弃,用0在尾补充)
  14. 13 | shiftright(-1, 5) 11111111 -> 00000111 7 --把整型往右移动指定的位数(超出的舍弃,用0在尾补充)

相关文章