Complete the code to check if a number is even using bit manipulation.
int isEven(int num) {
return (num & [1]) == 0;
}The least significant bit (LSB) of an even number is 0. Using num & 1 checks this bit.
Complete the code to multiply a number by 8 using bit manipulation.
int multiplyByEight(int num) {
return num [1] 3;
}Left shifting a number by 3 bits multiplies it by 23 = 8.
Fix the error in the code to divide a number by 4 using bit manipulation.
int divideByFour(int num) {
return num [1] 2;
}Right shifting by 2 bits divides the number by 22 = 4.
Fill both blanks to toggle the 3rd bit of a number.
int toggleThirdBit(int num) {
return num [1] [2];
}XOR (^) with 1 << 2 toggles the 3rd bit (bit index 2).
Fill all three blanks to set the 5th bit of a number and clear the 2nd bit.
int setAndClearBits(int num) {
num = num [1] [2];
num = num [3] ~(1 << 1);
return num;
}Use OR (|) with 1 << 4 to set the 5th bit, then AND (&) with the negation (~) of 1 << 1 to clear the 2nd bit.
