Skip to content

Instantly share code, notes, and snippets.

@justsml
Created June 14, 2013 13:31
Show Gist options
  • Save justsml/5781820 to your computer and use it in GitHub Desktop.
Save justsml/5781820 to your computer and use it in GitHub Desktop.
var car_options = 0x5; // binary 0101
var LEATHER_SEATS = 0x1; // 0001
var TURBO = 0x2; // 0010
var HID_LIGHTS = 0x4; // 0100
var SPORT_KIT = 0x8; // 1000
var daves_car = LEATHER_SEATS | HID_LIGHTS | SPORT_KIT; // 0001 | 0100 | 1000 => 1011 // 1 + 4 + 8 = 13
var dans_car = LEATHER_SEATS | TURBO | HID_LIGHTS | SPORT_KIT; // 0001 | 0010 | 0100 | 1000 => 1111 // 1 + 2 + 4 + 8 = 15
console.log("daves_car: " + daves_car); // 13
console.log("dans_car: " + dans_car); // 15
// Does Dave have LEATHER_SEATS?
console.log("Does Dave have LEATHER_SEATS? " + ((LEATHER_SEATS & daves_car) === LEATHER_SEATS ? "YES" : "NO" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment