13 is 1101.1's are also commonly known as set bits.131 or 0. And at the end we count it from the bottom to top , i.e in reverse order from top. When counting the remainders, start from the least significant bit (the bottom) and read upward to form the binary representation. For the above example this looks like :2 | 13 | 1 |
|---|---|---|
2 | 6 | 0 |
2 | 3 | 1 |
1 | 1 |
1011, and now we count it in reverse order.32 bit datatype so 13 is known as 1101 but it actually is (00000…… 01101) where the total number of bits are 32 (i.e 28 zeroes before 1101).string decimalToBinary(int n) {
string binary = "";
while (n > 0) {
binary += (n % 2 == 1) ? '1' : '0'; // Append '1' if odd, else '0'
n /= 2; // Divide by 2 to process next bit
}
return binary;
}#define numtobin(n) bitset<32>(n).to_string()
/* string z = numtobin(n) gives z as binary of n but not reversed
This macro is pseudo-code and may require adjustments
for specific C++ implementations. */reverse(s.begin(),s.end()); function in the end to reverse the string.int binaryToDecimal(string binary) {
int decimal = 0, power = 1; // power represents 2^i
for (int i = binary.length() - 1; i >= 0; i--) {
if (binary[i] == '1')
decimal += power; // Add power if bit is '1'
power *= 2; // Move to the next power of 2
}
return decimal;
}int binaryToDecimal(string binary) {
int power = 1, decimal = 0; // power represents 2^i
for (int i = binary.length() - 1; i >= 0; i--) {
if (binary[i] == '1')
decimal += power; // Add power if bit is '1'
power *= 2; // Move to the next power of 2
}
return decimal;
}#define bintoint(bin_str) stoi(bin_str, nullptr, 2)
// int z = bintoint(s); returns int form of bitwise s stored in z.~ we use which flip the bits which is discussed below.13 looks like :
1 to its one’s complement i.e one’s complement + 1 ; like for 0010 it will be 0011.0011 we get 0100 , as it looks like :
1 gets carried over during adding + 1.0 for Positive1 for Negative2’s complement on it.-5 we first take the bit value of 5 in a 8 bit system for instance i.e 00000101.11111010. Add 1 – 11111011. Which results in -1 (11111011).(01111…1) , 0 at start as the first element represents the integer being positive. The value of (01111..1) is known as INT_MAX i.e the maximum value int can store which is .(1000…0) which is .(1101) & 7 (0111) to get 5 (0101).
(1101) and 7 (0111) to get 15 (1111).
a = a ^ b is read as - (a is equal to a XOR b)a = (b ^ a) ^ a gives us b. i.e (b ^ a ) ^ a = b.a ^ a = 0
13 >> 1 means removing one bit from the right of 13. i.e 13 is 1101 so 13 >> 1 = 110which is 6. Similarly 13 >> 2 = 11 which is 3.x >> k = x/2k, Like 13 >> 2 = 13/22 = 3.13 << 1
13 << 1 = 26 i.e (11010).
n << k = n * 2kINT_MAX >> 1 then this causes an overflow.(110) = (001).if (n & 1)
cout << "Odd";
else
cout << "Even";a and b we use just use the approach :a = a ^ b;
b = a ^ b;
a = a ^ b;13 is set, we traverse from the back of the number in bitset format.N,((N & (1 << i)) != 0) then it’s a set bit, else its not.1 << i in which 1 is at the ith position of the number so on using (N & the number) we get 1 at that position in the resultant only if it’s a set.class Solution {
public:
int hammingWeight(int n) {
return __builtin_popcount(n); // Counts the number of set bits (1s)
}
};(n & (n - 1)) == 0 and also account for the edge cases such as n = 0 and n = INT_MIN.class Solution {
public:
bool isPowerOfTwo(int n) {
return n == 0 ? false : n == INT_MIN ? false : (n&(n-1)) == 0;
}
};N and the desired number it has to be converted to, the resultant we receive after using XOR on these two numbers has the same number of 1’s as the numbers of bitflips we are supposed to do. i.e after XOR’ing the two numbers we can run a for loop traversing through the 31 bits and check
(ans& (1 << i)) then cnt++.int countBitFlips(int N, int M) {
int xor_result = N ^ M; // XOR gives bits that are different
int cnt = 0;
for (int i = 0; i < 31; i++) {
if (xor_result & (1 << i)) { // Check if the i-th bit is set
cnt++;
}
}
return cnt;
}2 upto N and initialise a list and check keep checking in the loop if the number is a factor of N and if it is a prime, if it satisfies both these conditions then it is added to this list.
vector<int> getPrimeFactors(int n) {
vector<int> factors;
// Check divisibility by 2
if ((n & 1) == 0) {
factors.push_back(2);
while ((n & 1) == 0) n >>= 1; // Divide by 2 using right shift
}
// Check for odd factors
for (int i = 3; i * i <= n; i += 2) {
while ((n % i) == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 1) factors.push_back(n); // If n is prime
return factors;
}n > 1, add n as a factor to ensure that the prime factor (which might be greater than ) is included.2 is added , any multiple of 2 will not be added in the list. But as this loop is only traversing upto it won’t count N being a prime factor of itself so we run an if loop in the end to check if the number is a prime factor of itself and add it to the list.a ^ a = 0 so we can just XOR all the numbers and the duplicates will be XOR’ed to give 0 whereas the single number will be the only remaining one.class Solution {
public:
int singleNumber(vector<int>& nums) {
int xorr = 0;
for (int i = 0 ; i < nums.size() ; i++) {
xorr = xorr ^ nums[i];
}
return xorr;
}
};class Solution {
public:
vector<int> twoOddNumbers(vector<int>& nums) {
int xorr = 0;
for (int num : nums) {
xorr ^= num;
}
// Find the rightmost set bit
int rightmost_bit = xorr & -xorr;
int num1 = 0, num2 = 0;
// Divide numbers into two groups
for (int num : nums) {
if (num & rightmost_bit) num1 ^= num;
else num2 ^= num;
}
return {num1, num2};
}
};1 upto N and keep XOR’ing it , this hasN % 4 == 0 then ans = N.N % 4 == 1 then ans = 1.N % 4 == 2 then ans = N + 1.N % 4 == 3 then ans = 0.1 , instead the given range is [L,R] then we can basically XOR two numbers using the above method , the two numbers being L - 1 and R.(1 ^ 2 ^ 3 ^ 4 ^ 5) ^ (1 ^ 2 ^ 3) == (4 ^ 5) as XOR’ing the same number gives us 0 , so we can XOR L - 1 and R to get the resultant XOR , i.e L - R.