Hello:
I’m not able to find a left/right shift operator like in C: 8 >> 1 = 4.
I can use div 2, 4 8 etc. But is there a shift function/operator?
Thanks in advance.
Hello:
I’m not able to find a left/right shift operator like in C: 8 >> 1 = 4.
I can use div 2, 4 8 etc. But is there a shift function/operator?
Thanks in advance.
Hi
You can achieve shifting using vs/sv and prev/next. In example below x is the number of shifts and y is the starting number:
q){0b sv prev/[x;0b vs y]}[1;8]
4
“0b vs” will convert a number to its binary representation, “0b sv” is the reverse operation:
q)0b vs 8
0000000000000000000000000000000000000000000000000000000000001000b
q)0b sv 0b vs 8
8
The operators prev and next will move the sequence:
q)next 0b vs 8
0000000000000000000000000000000000000000000000000000000000010000b
q)prev 0b vs 8
0000000000000000000000000000000000000000000000000000000000000100b
Hope this helps.
Thomas Smyth
AquaQ Analytics
Thanks Thomas.
I don’t know sv can be used on numbers before.
Meanwhile, I find a solution based on integer multiple and division.
lshift: { x * prd y # 2 }
rshift: { x div prd y # 2 }
Also I’m wondering why this operator is not provided by kdb+, given that there is a x86 assembly instruction for it.
Regards
Hao