Skip to content

Instantly share code, notes, and snippets.

@pmolina
Last active September 1, 2019 22:05
Show Gist options
  • Save pmolina/3bb4a81c6d7fb9551c730c76083eaa59 to your computer and use it in GitHub Desktop.
Save pmolina/3bb4a81c6d7fb9551c730c76083eaa59 to your computer and use it in GitHub Desktop.
// *** 1 ***
var flavors = [
{
"id":1,
"name":"Chocolate"
},
{
"id":2,
"name":"Frutos del bosque"
},
{
"id":3,
"name":"Vainilla"
},
{
"id":4,
"name":"Tramontana"
}
];
// *** 2 ***
for (var i in flavors) {
console.log(1 << flavors[i]["id"]);
}
// *** 3 ***
for (var i in flavors) {
console.log(1 << flavors[i]["id"] - 1);
}
// *** 4 ***
for (var i in flavors) {
console.log(flavors[i]["name"] + ": " + flavors[i]["id"] + " -> " + (1 << flavors[i]["id"] - 1).toString(2));
}
// *** 5 ***
var db = 0; // Mi base de datos arranca en cero, no tiene ningún gusto.
db = db | 1 << (1 - 1); // Agrego que tengo Chocolate
db = db | 1 << (4 - 1); // Agrego que tengo Tramontana
console.log(db);
// *** 6 ***
db |= 1 << (2 - 1); // Agrego que entró Frutos del bosque
// *** 7 ***
db &= ~(1 << (1 - 1)); // Me quedé sin Chocolate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment