Skip to content

Instantly share code, notes, and snippets.

@ElenaG518
Last active January 9, 2019 01:54
Show Gist options
  • Save ElenaG518/8ee2b385b532fd440d57d7d6b8602f72 to your computer and use it in GitHub Desktop.
Save ElenaG518/8ee2b385b532fd440d57d7d6b8602f72 to your computer and use it in GitHub Desktop.
Logical Operators
Write a function that takes an integer value and checks to see if it is even or odd using the bit-wise AND operator.
Hint: Think about what the value of the least-significant bit will be for even and odd numbers.
function evenOrOdd(int) {
if (int & 1) {
console.log(`${int} is an odd number`);
} else if (!(int & 1)) {
console.log(`${int} is an even number`);
}
}
https://repl.it/@elenaG518/evenOrOdd
Why would using bit-wise operations be potentially faster for checking whether a number
is even or odds as opposed to using something like the modulo operator (for example randInt % 2)?
it is faster to look at the last bit of a binary sequence since it does not require any type of conversion into
its integer equivalent, nor does it require to do the modulo operation on said integer.
// Write a function that takes in two integer values and prints out the resultant value when you AND the two input values
and then also when you OR the two input values.
// Extend the previous function further by adding logic for the XOR operation when two integer values are input.
Add a third parameter which denotes which type of operation to execute. Print out the resultant value for the
associated operation type.
function compareTwo(a, b, funct) {
// console.log("here ", a, b, funct);
if (funct === "&") {
console.log("and ", a & b);
} else if (funct == "|") {
console.log("or ", a|b);
} else if (funct == "^" ) {
console.log("^OR ", a ^ b);
};
};
// Write a function that takes in an integer value and prints out its complement value.
console.log(~4);
Interview questions:
Write a function which sets the third bit of a number.
function setThird(num) {
num = num | 4;
console.log("setThird ", num);
};
explanation:
// convert any number to taht number + 4, which is the third bit of a number. example 10 + 4 = 14
// 1010 convert to 1110
// 1010
// ????
// ----
// 1110
// & will only work if the number (10) already has a 1 in its third bit
// | will work regardless if number already has a 1 in its third bit (10 or 14 will still result in 14 if we set the third bit).
// ^ will not work if the number already has the third bit set, becuase if we add 4(equivalent of setting third bit to 1) to the number, it will turn the 3rd bit to 0, as in 20 will turn to 16 like in toggleThird
Write a function which toggles the third bit of a number.
function toggleThird(num) {
num = num ^ 4;
console.log("toggleThird ", num);
};
explanation:
num ~4 will turn the 3rd bit to only zero, so that wont work.
| operator will not work becuase if 1 & 1 is still 1.
& wont work becaues 0 & 1 is 0 and 1&1 is 1 (not toggled), etc.
^ will work becuase 0^1 is 1 and 1^1 is 0
Write a function which clears (sets to zero) the third bit of a number.
function clears(num) {
num = num & ~4;
console.log("clears ", num);
};
explanation:
num ~4 will turn the 3rd bit to only zero
Write a function which tells you whether the third bit of a number is set.
function isSet(num) {
if(num & 4) {
console.log(true);
} else {
console.log(false);
}
};
explanation:
if the third bit of a number is 1 then that means 1*[2^2] which equals 4. therefore the number can be broken down into
the addition of 4 plus another integer to equal the original number.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment