Skip to content

Instantly share code, notes, and snippets.

@tzi
Created March 28, 2012 21:26
Show Gist options
  • Save tzi/2230728 to your computer and use it in GitHub Desktop.
Save tzi/2230728 to your computer and use it in GitHub Desktop.
A simple example of #bit operator
window.onload = function(){
Compass = { North:1, South:2, Est:4, West:8 };
var move = function(step) {
dir = [];
if (step & Compass.North) dir.push('North');
else if (step & Compass.South) dir.push('South');
if (step & Compass.Est) dir.push('Est');
else if (step & Compass.West) dir.push('West');
document.write( step + ': ' + dir.join(' ') + '<br/>' );
}
move( 1 ); // North (1)
move( 2 ); // + South (2)
move( 4 ); // + + Est (4)
move( 5 ); // North (1) + + Est (4)
move( 6 ); // + South (2) + Est (4)
move( 8 ); // + West (8)
move( 9 ); // North (1) + + + West (8)
move( 10); // + South (2) + + West (8)
}
<!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