In the previous topic, we got an introduction to Bit Manipulation in DSA. Let us now look at some examples using bit manipulation.
x
and y
, return the Hamming distance between them:Note: The Hamming distance between two integers is defined as the number of points where the corresponding bits differ.
i.e,
Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to position where the corresponding bits are different.
Solution:
class Solution {
public int hammingDistance(int x, int y) {
int xor = x ^ y; // perform bitwise XOR to get the difference
int distance = 0;
while (xor != 0) {
if ((xor & 1) == 1) { // check if the rightmost bit is set
distance++; // increment the distance counter
}
xor >>>= 1; // shift right by one bit
}
return distance;
}
}
a
and b
, return their sum as a binary string.i.e,
Input: a = "11", b = "1" Output: "100"
Solution:
class Solution {
public String addBinary(String a, String b) {
int x = Integer.parseInt(a, 2);
int y = Integer.parseInt(b, 2);
int carry = 0;
while (y != 0) {
carry = x & y;
x = x ^ y;
y = carry << 1;
}
return Integer.toBinaryString(x);
}
}
dividend
and divisor
, divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing dividend
by divisor
:i.e,
Input: dividend = 10, divisor = 3 Output: 3 Explanation: 10/3 = 3.33333.. which is truncated to 3
Solution:
class Solution {
public int divide(int A, int B) {
if (A == 1 << 31 && B == -1) return (1 << 31) - 1;
int a = Math.abs(A), b = Math.abs(B), res = 0, x = 0;
while (a - b >= 0) {
for (x = 0; a - (b << x << 1) >= 0; x++);
res += 1 << x;
a -= b << x;
}
return (A > 0) == (B > 0) ? res : -res;
}
}
true
if it is a power of two. Otherwise, return false
:i.e,
Input: n = 16 Output: true Explanation: 24 = 16
Solution:
class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}
}
nums
, every element appears twice except for one. Find that single one.i.e,
Input: nums = [4,1,2,1,2] Output: 4
Solution:
class Solution {
public int singleNumber(int[] nums) {
int result = 0;
for (int num : nums) {
result ^= num;
}
return result;
}
}
Note: also read about DSA: Bits Manipulation
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…