Skip to content

Instantly share code, notes, and snippets.

@tzi
Created March 28, 2012 21:44
Show Gist options
  • Save tzi/2230846 to your computer and use it in GitHub Desktop.
Save tzi/2230846 to your computer and use it in GitHub Desktop.
A second example of #bit operator
window.onload = function(){
Compass = { North:131, South:56, West:224, Est:14 };
var move = function(step) {
dir = [];
var pow = Math.pow( 2, step);
if ( pow & Compass.North ) dir.push('North');
if ( pow & Compass.South ) dir.push('South');
if ( pow & Compass.Est ) dir.push('Est' );
if ( pow & Compass.West ) dir.push('West' );
document.write( step + ': ' + dir.join(' ') + '<br/>' );
}
move( 0 );
move( 1 );
move( 2 );
move( 3 );
move( 4 );
move( 5 );
move( 6 );
move( 7 );
}
<!DOCTYPE html>
<html>
<head>
<title>Bit operator</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript" src="direction.js"></script>
<body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment