Day 29: Bitwise AND In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 29: Bitwise AND in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 29: Bitwise AND – Hacker Rank Solution

Day 29: Bitwise AND – Hacker Rank Solution


Welcome to the last day! Today, we’re discussing bitwise operations. Check out the Tutorial tab for learning materials and an instructional video!

Given set . Find two integers,  and  (where ), from set  such that the value of  is the maximum possible and also less than a given integer, . In this case,  represents the bitwise AND operator.

Function Description
Complete the bitwiseAnd function in the editor below.

bitwiseAnd has the following paramter(s):
– int N: the maximum integer to consider
– int K: the limit of the result, inclusive

– int: the maximum value of  within the limit.

Input Format

The first line contains an integer, , the number of test cases.
Each of the  subsequent lines defines a test case as  space-separated integers,  and , respectively.


Sample Input

STDIN   Function
-----   --------
3       T = 3
5 2     N = 5, K = 2
8 5     N = 8, K = 5
2 2     N = 8, K = 5

Sample Output



All possible values of  and  are:

The maximum possible value of  that is also  is , so we print  on a new line.

Day 29: Bitwise AND – Hacker Rank Solution
import java.util.Scanner;

 * @author Techno-RJ
public class Day29BitwiseAND {
	public static void main(String[] args) {
		Scanner in = new Scanner(;
		int t = in.nextInt();
		for (int a0 = 0; a0 < t; a0++) {
			int n = in.nextInt();
			int k = in.nextInt();
			if (((k - 1) | k) <= n) {
				System.out.println(k - 1);
			} else {
				System.out.println(k - 2);


28 thoughts on “Day 29: Bitwise AND In Java | 30 Days Of Code | Hackerrank Programming Solutions”

  1. Hello great blog! Does running a blog such as this require a massive amount work?
    I have virtually no knowledge of computer programming
    however I had been hoping to start my own blog soon. Anyway, should you have
    any ideas or tips for new blog owners please share. I understand this
    is off subject nevertheless I simply had to ask. Thank you!


Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock