Bit Shifting

Question: Bit shifting is a low level operation that shifts all the bits of a number to either the left or right direction. Shifting a number's bits one place to the left will double the number. Shifting a number's bits one place to the right will halve the number. This bitwise operation performs much faster than the standard multiplication/division operations. Write the functions 'lshift' and 'rshift' in C that takes in two q objects (list or single long integer to represent number(s) to shift, single long integer to represent number of bits to shift) and shifts the number(s) that many bits to the left or right, respectively. This function should be exportable to q.

More Information:

https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts

Example

                                
                                q).q.lshift:`shift 2: (`lshift;2) 
q).q.rshift:`shift 2: (`rshift;2) 
q)3 lshift 2 
12 
q)12 rshift 2 
3 
q)\t do[1000000;{x*2}/[20;1 2 3 4 5 6 7 8 9]] 
1958 
q)\t do[1000000;lshift[1 2 3 4 5 6 7 8 9;20]] 
86 
q)({x*2}/[20;1 2 3 4 5 6 7 8 9])~lshift[1 2 3 4 5 6 7 8 9;20] 
1b
                                
                            

Solution

Tags:
c
Searchable Tags
algorithms api architecture asynchronous c csv data structures dictionaries disk feedhandler finance functions ingestion ipc iterators machine learning math multithreading optimizations realtime shared library sql statistics streaming strings tables temporal utility websockets