Java Program to Check if K-th Bit is Set or Not8 May 2025 | 4 min read In computer programming, numbers are usually represented in binary, where each bit (digit) can be either 0 or 1. The k-th bit of a number corresponds to the bit at position k, with the rightmost bit (least significant bit) being counted as position 0. To determine whether the k-th bit is set (1) or not set (0), we use bitwise operations. These operations allow us to efficiently inspect and manipulate individual bits of a number. Problem StatementWe have given a number (n)and a bit number (k). We have to check if the kth index bit of number n is set or not. The position of bit should be indexed starting with 0 from the LSB side in the binary representation of the number. Set: if bit is 1 Not set: if it is 0 Let's understand it through an example. Example of K-th Bit is Set or NotExample 1: Input: n=7, k=2 Output: SET Explanation: 7 is represented as 111 in binary. The bit at position 2 (counting from 0) is 1 which is SET. Example 2: Input>: n=4, k=0 Output>: NOT SET Explanation>: 4 is represented as 100 in binary. The bit at position 0 (counting from LSB) is 0, so it is NOT SET. Approach: Using Left Shift OperationThis method utilizes the left shift operator (<<) to generate a mask with the k-th bit set. A bitwise AND operation (&) is then applied between the number and the mask to determine whether the k-th bit is set or not. AlgorithmStep 1: Shift 1 left by k positions to create a mask for the k-th bit. Step 2: Perform bitwise AND operation between the number n and the mask created. Step 3: If the outcome is not zero, it signifies that the k-th bit is activated to 1. Step 4: If the outcome is zero, it indicates that the k-th bit is not configured to 1. Step 5: Print "SET" if the k-th bit is active; otherwise, print "NOT SET". Let's implement the above algorithm in a Java program. Output: The 3-th bit is SET. Time Complexity: The time complexity of the above code is O(1). Auxiliary Space Complexity: The auxiliary space complexity of the above code is O(1). Approach: Using Right Shift OperatorThis method employs the right shift operator (>>) to shift the k-th bit to the least significant position. Then, the least significant bit is checked to see whether the k-th bit is set or not. AlgorithmStep 1: Right shift the number n by k positions using n >> k. Step 2: The least significant bit (LSB) of the result will be the k-th bit of n. Step 3: If the result of n >> k is odd, the k-th bit is set; otherwise, it's not. Step 4: Print "SET" if the least significant bit is 1, otherwise print "NOT SET". Let's implement the above algorithm in a Java program. File Name: CheckKthBit.java Output: The 3-th bit is SET. Time Complexity: The time complexity of the above code is O(1). Auxiliary Space Complexity: The auxiliary space complexity of the above code is O(1). Next TopicUndulating Number in Java |
Introduction Backtracking is an algorithmic technique that utilizes a brute-force approach to find the desired solution. Put simply, it exhaustively tries all possible solutions and selects the optimal one. The term backtracking refers to the process of retracing one's steps and exploring other alternatives if the current...
7 min read
Java is a widely-used, versatile programming language known for its portability and reliability. However, like any programming language, it can throw errors that may seem cryptic to newcomers. One such error is the "Invalid Target Release: 9" error. In this section, we will explore the meaning...
6 min read
Elasticsearch is a full-text search and investigation motor in view of Apache Lucene. Elasticsearch makes it more straightforward to perform data aggregation procedures on data from different sources and to perform unstructured queries, for example, Fluffy Hunts, on the put away information. It stores information in a...
6 min read
Java is a general-purpose, robust, secure, and object-oriented programming language. It is a high-level language, I.e., its syntax uses English like language. It was developed by Sun Microsystems in the year 1995. It is now maintained and distributed by Oracle. Java has its runtime environment and...
3 min read
In the world of programming, basic arithmetic operations like addition are often taken for granted. We rely on operators like '+' to perform addition effortlessly. However, have you ever wondered how to add two numbers without using any operators in Java? In this section, we will...
8 min read
Fundamental ideas called serialization and deserialization are used to convert Java objects into a format that may be quickly transmitted, stored, or recreated. Serialization Serialization is the process of converting an object into a byte stream so that it may be sent over a network, saved in a...
4 min read
In the world of Java programming, developers often encounter the terms "container" and "component." These terms are fundamental to Java's graphical user interface (GUI) development, and understanding their distinctions is crucial for creating robust and modular applications. In this section, we will explore the key differences...
4 min read
In the world of Java programming, graphical user interfaces (GUIs) play a crucial role in providing a user-friendly and interactive experience. GUI components are the building blocks of these interfaces, allowing developers to design and create sophisticated applications. Among these components, two fundamental concepts stand out:...
3 min read
In the input, a large number (in the form of a string) is given to us. We need to divide it by another number (in the form of an int data type). Our task is to find the division of these numbers and return the...
3 min read
Web data extraction, sometimes referred to as web harvesting or web scraping, is a method for obtaining information from websites. Because of its strong libraries and adaptability, Java is a popular programming language for jobs involving web scraping. In this section, we will discuss web scrapping...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India