Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.