Bitwise Algorithms and Manipulations

Last Updated :
Discuss
Comments

Question 1

Which bitwise operator is used to swap two numbers?

  • A

    Left Shift(<<)

  • B

    Right shift(>>)

  • C

    Bitwise OR |

  • D

    Bitwise XOR ^

Question 2

What is the output of the below code for input num = 15?

C++
unsigned int fun(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
C
unsigned int fun(unsigned int n) {
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
Java
public class Main {
    public static int fun(int n) {
        int count = 0;
        while (n != 0) {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
}
Python
def fun(n):
    count = 0
    while n:
        count += n & 1
        n >>= 1
    return count
JavaScript
function fun(n) {
    let count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
  • A

    15

  • B

    5

  • C

    4

  • D

    3

Question 3

Which is the format specifier used to prefix 0x and print a number in hexadecimal notation.?

  • A

    %O

  • B

    %#

  • C

    %x

  • D

    %#x

Question 4

Which is the Bit Toggling operator below.?

  • A

    Bitwise OR operator( | )

  • B

    Bitwise XOR Operator (^)

  • C

    Bitwise AND Operator(&)

  • D

    TILDE operator(~)

Question 5

What is the output of the below expression? 

000111 | 01010
  • A

    0000000

  • B

    00

  • C

    010111

  • D

    None

Question 6

Bitwise & can be used in conjunction with ~ operator to turn off 1 or more bits in a number.

  • A

    True

  • B

    False

Question 7

If we have to add two numbers without having any carry, which bitwise operator should be used?

  • A

    Bitwise OR |

  • B

    Bitwise AND &

  • C

    Bitwise NOT ~

  • D

    None

Question 8

Predict the output:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = -5;
    x = x >> 1;
    cout << x << endl;
    return 0;
}
C
#include <stdio.h>

int main() {
    int x = -5;
    x = x >> 1;
    printf("%d\n", x);
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int x = -5;
        x = x >> 1;
        System.out.println(x);
    }
}
Python
# x is initialized to -5
x = -5
# Right shift operation
x = x >> 1
# Print the result
print(x)
JavaScript
// x is initialized to -5
let x = -5;
// Right shift operation
x = x >> 1;
// Print the result
console.log(x);
  • A

    -2

  • B

    -3

  • C

    Error

  • D

    -1

Question 9

What is the output of the below code?

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    if (~0 == 1)
        cout << "Yes";
    else
        cout << "No";
}
C
#include <stdio.h>
int main() {
    if (~0 == 1)
        printf("Yes");
    else
        printf("No");
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        if (~0 == 1)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
Python
if ~0 == 1:
    print("Yes")
else:
    print("No")
JavaScript
if (~0 === 1)
    console.log("Yes");
else
    console.log("No");
  • A

    Yes 

  • B

    No

  • C

    Error

  • D

    None

Question 10

What will the below code do?

C++
void function(int& num, int pos) 
{
   num |= (1 << pos); 
}
C
void setBit(int* num, int pos) {
   *num |= (1 << pos); 
}
Java
void setBit(int[] num, int pos) {
   num[0] |= (1 << pos); 
}
Python
def set_bit(num, pos):
    return num | (1 << pos)
JavaScript
function setBit(num, pos) {
   return num | (1 << pos);
}
  • A

    divide num by pos.

  • B

    Multiply num by pos.

  • C

    Add num with pos.

  • D

    set the given position (pos) of a number (num).

There are 30 questions to complete.

Take a part in the ongoing discussion