Bitwise operations for numbers
by Alon Rotem
HTML
<div id="results">
</div>
CSS
html
{
font-family: verdana;
}
JavaScript
//Bitwise operations assume int32s, so works until (2^31)−1 = 2,147,483,647
function writeLine(text)
{
$("#results").html($("#results").html() + text + "<br/><br/>");
}
writeLine("<b>Works!</b> :-) 300.1 | 0 = " + (300.1 | 0));
writeLine("<b>Doesn't work!</b> :-( 3000000000.1 | 0 = " + (3000000000.1 | 0));
writeLine("<b>Double tilda:</b> ~~3000.1=" + (~~3000.1));
//will not work, because 1&0=0 and 0&0=0
writeLine("<b>Bitwise AND:</b> 3000.1 & 0 = " + (3000.1 & 0));
writeLine("<b>Will work up to </b>" + ((Math.pow(2,31)-1)));