You are on page 1of 5

BIT Practice Questions

Question 1:
Given an array of integers, find the maximum bitwise AND value of any two integers in the array.
Input: [3, 5, 8, 10, 12]
Output: 8
Explanation: The maximum bitwise AND value of any two integers in the array is 8, which is the
result of the bitwise AND operation between 10 (1010 in binary) and 12 (1100 in binary).

Code :

public class MaximumBitwiseAnd {

public static void main(String[] args) {

int[] nums = {3, 5, 8, 10, 12};

int result = findMaximumBitwiseAnd(nums);

System.out.println("Maximum Bitwise AND value: " + result);

public static int findMaximumBitwiseAnd(int[] nums) {

int maxBitwiseAnd = 0;

for (int i = 0; i < nums.length; i++) {

for (int j = i + 1; j < nums.length; j++) {

int bitwiseAnd = nums[i] & nums[j];

maxBitwiseAnd = Math.max(maxBitwiseAnd, bitwiseAnd);

return maxBitwiseAnd;

Output:
Question 2 :
Given a non-negative integer n, find the number of integers x such that 0 <= x <= n and n & x
== x
Input: n = 5
Output: 4
Explanation: The integers x that satisfy the condition are 0, 1, 4, and 5.

Code :

public class BitwiseCount {

public static void main(String[] args) {

int n = 5;

int count = findBitwiseCount(n);

System.out.println("Number of integers satisfying the condition: " + count);

public static int findBitwiseCount(int n) {

int count = 0;

for (int x = 0; x <= n; x++) {

if ((n & x) == x) {

count++;

}
return count;

Output :

Question 3 :-

Palindromic binary representation ;-

Code :

public class PalindromicBinary {

public static void main(String[] args) {

int A = 9;

int result = findAthPalindromicBinary(A);

System.out.println("Ath number with palindromic binary representation: " + result);

public static int findAthPalindromicBinary(int A) {

int count = 0;

int num = 0;
while (count < A) {

num++;

if (isPalindromicBinary(num)) {

count++;

return num;

public static boolean isPalindromicBinary(int num) {

String binary = Integer.toBinaryString(num);

int left = 0;

int right = binary.length() - 1;

while (left < right) {

if (binary.charAt(left) != binary.charAt(right)) {

return false;

left++;

right--;

return true;

Output :

You might also like