Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain 1<<16 | 10?

Tags:

ruby

I need to use the code below, but I'm new to ruby and programming in general and don't understand what this does. Can someone tell me what this does or at least what this is called?

def MAKELPARAM(w1, w2)
  return (w2<<16) | w1
end

Thanks!

like image 861
Lifeweaver Avatar asked Jan 23 '26 23:01

Lifeweaver


2 Answers

This performs a left-shift of w2 by 16 bits, then bitwise-or's w1 into the result.

like image 73
Jeff Paquette Avatar answered Jan 26 '26 17:01

Jeff Paquette


So those could be bitwise operators if you know that they are numbers however if the params coming in are arrays then << is the operator to add to the array. Also then the | operator does an or on the two array's returning an array of the elements that are in either array
For Example:

w1 =[]
w2 = [16,13]
w3 = [13]
MAKELPARAM(w1,w2)
#Returns [16,13]

MAKELPARAM(w1,w3)
#Returns [16,13]

MAKELPARAM(w1,w1)
#Returns [16]
like image 34
Mgrandjean Avatar answered Jan 26 '26 19:01

Mgrandjean