Skip to content

Instantly share code, notes, and snippets.

@pmolina pmolina/icecream.js
Last active Sep 1, 2019

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