Category Archives: Bit Magic

Check if a number has bits in alternate pattern

Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101. Print “Yes” if it has an alternate pattern otherwise “No”. Here alternate pattern can be like 0101 or 1010. Examples: Input : 15 Output… Read More »

Subset sum queries using bitset

Given an array arr[] and a number of queries, where in each query we have to check whether a subset whose sum is equal to given number exists in the array or not. Examples: Input : arr[] = {1, 2, 3}; query[] = {5, 3, 8} Output : Yes, Yes, No There is a subset… Read More »

Bitwise and (or &) of a range

Given two non-negative long integers, x and y given x <= y, the task is to find bit-wise and of all integers from x and y, i.e., we need to compute value of x & (x+1) & … & (y-1) & y.7 Examples: Input : x = 12, y = 15 Output : 12 12… Read More »

Multiples of 4 (An Interesting Method)

Given a number n, the task is to check whether this number is a multiple of 4 or not without using +, -, * ,/ and % operators. Examples: Input: n = 4 Output – Yes n = 20 Output – Yes n = 19 Output – No Method 1 (Using XOR) An interesting fact… Read More »