DEV Community

Discussion on: Explain bitwise operator Like I'm Five

Collapse
 
drbearhands profile image
DrBearhands

IIRC you cannot assume what number primitives correspond to what bits nor that shifting corresponds to dividing and multiplying by 2. Big/little endian aside, the spec doesn't enforce it (in every language). That's one of the reasons we have boolean operators in the first place.

Collapse
 
lexlohr profile image
Alex Lohr

While this may not be the case in every language, at least JavaScript got you covered:

Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if not only the low five bits will be used (see developer.mozilla.org/en-US/docs/W...).