Invert k-th Most Significant Bits of N in Java9 May 2025 | 3 min read Problem StatementInverting the k-th most significant bit (MSB) of a number N involves flipping the bit at position k, counting from the leftmost bit. Solution to The ProblemThe process is as follows:
For example, for N = 59 (binary 111011) and k = 3, inverting the 3rd bit results in 51 (binary representation 110011). This technique is useful in bitwise operations for tasks like flag manipulation. Example 1: Input: N = 10, K = 1 Output: 2 The binary form of 10 is 1010. When the first bit is inverted, it turns into 0010, which has a decimal value of 2. Example 2: Input: N = 32, K = 2 Output: 48 The binary form of 32 is 100000. By flipping the second bit, it changes to 110000, which has a decimal value of 48. Bit Manipulation ApproachFirst, determine the total number of bits in N. If the number of bits is less than K, return N as the result. Otherwise, flip the K-th most significant bit of N and then return the new number obtained after the flip. AlgorithmStep 1: Determine the amount of bits needed for n's binary representation and save the binary digits in an array. Step 2: If k is greater than the total bits in n, return n unchanged. Step 3: Alter the k-th bit in the binary array by switching its value (from 0 to 1 or from 1 to 0). Step 4: Calculate the decimal value of the updated binary array. Step 5: Output the decimal value obtained after flipping the k-th bit. ImplementationOutput: 37 Time and Space Complexity: O((log(m)+log(n)) |
Bitwise Operators in Java
In Java, bitwise operators are used to execute binary number bit-level operations. These operators alter the bits in a number by performing operations like bit shifting, AND, OR, NOT, and XOR. With examples and programs, we will go over the various types of bitwise operators...
5 min read
Implement Quintet Class with Quartet Class in Java using JavaTuples
The JavaTuples library introduces a powerful mechanism for managing structured data using tuples-ordered collections of elements in Java programming. Among its notable components are the Quartet and Quintet classes, each designed to handle tuples with four and five elements, respectively. These generic classes allow developers to...
10 min read
ClassNotFoundException Java
Java is a popular programming language used by developers around the world to build a wide range of applications. Despite its popularity and reliability, Java programs are prone to errors and exceptions. One of the most common exceptions in Java is the ClassNotFoundException. In this article,...
4 min read
Maximum difference of Zeros and Ones in Binary String Using Java
Problem Statement Given a binary string we need to find the maximum difference of 0s and 1s inside the given binary string. Here, you treat 0 as +1 and 1 as -1, and then seek the maximum value of contiguous subarray. This maximum sum of a subarray...
4 min read
AES GCM Encryption Java
Two advantages of the widely used symmetric key encryption technique called AES-GCM (Advanced Encryption Standard-Galois/Counter Mode) are data integrity and privacy. We will look at using AES-GCM encryption in Java in this section. AES-GCM is a data block-based encryption method that belongs to the AES family. It...
4 min read
CollationElementIterator secondaryOrder() method in Java with Examples
The java.text.CollationElementIterator has a secondaryOrder() method. Every Collation element of a CollationElementIterator object has a secondary component, which is provided by the CollationElementIterator class. Syntax: public static final short secondaryOrder(int order) Parameter: The above method requires the secondary component to be located for a collation element...
4 min read
JavaCC
Like YACC, is also a parser. is a short form for Java Compiler-Compiler. It is an open-source popular parser generator and lexical analyzer generator tool developed by Oracle Corporation. It is written in the Java programming language. It is licensed under the BSD license....
6 min read
Kaprekar Number in Java
In the realm of number theory, Kaprekar numbers hold a special place due to their intriguing properties. Named after the Indian mathematician D. R. Kaprekar, these numbers possess the unique characteristic that they can be split into two parts, whose squares can be added to yield...
5 min read
New Date-Time API in Java 8
The Old java.Date class was actually not very rich in functionality and Java 8 came up with new Date-Time API. util. Date and java. util. Calendar classes. Earlier classes were not invariant; they were not design for multithreads and had complicated and error-prone methods. In order...
4 min read
Structured Concurrency in Java
Concurrency is a fundamental concept in modern software development, allowing programs to execute multiple tasks simultaneously. Java, one of the most popular programming languages, provides robust support for concurrent programming. In recent years, structured concurrency has emerged as a powerful paradigm for writing concurrent code in...
6 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