Skip to content

Instantly share code, notes, and snippets.

@ghalimi
Last active December 10, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghalimi/4488961 to your computer and use it in GitHub Desktop.
Save ghalimi/4488961 to your computer and use it in GitHub Desktop.
BITLSHIFT Function
// Copyright (c) 2012 Sutoiku, Inc. (MIT License)
function BITLSHIFT(number, shift) {
// Return error if either number is a non-numeric value
if (isNaN(number) || isNaN(shift)) return '#VALUE!';
// Return error if number is less than 0
if (number < 0) return '#NUM!';
// Return error if number is a non-integer
if (Math.floor(number) !== number) return '#NUM!';
// Return error if number is greater than (2^48)-1
if (number > 281474976710655) return '#NUM!';
// Return error if the absolute value of shift is greater than 53
if (Math.abs(shift) > 53) return '#NUM!';
// Return number shifted by shift bits to the left or to the right if shift is negative
return (shift >= 0 ) ? number << shift : number >> -shift;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment