# Bit Algorithms

Question 1 |

What is the return value of following function for arr[] = {9, 12, 2, 11, 2, 2, 10, 9, 12, 10, 9, 11, 2} and n is size of this array.

int fun(int arr[], int n) { int x = arr[0]; for (int i = 1; i < n; i++) x = x ^ arr[i]; return x; }

0 | |

9 | |

12 | |

2 |

**Bit Algorithms**

**Discuss it**

Question 1 Explanation:

Note that 9 is the only element with odd occurrences, all other elements have even occurrences.
If the input array has all elements with even occurrences except one, then the function returns the only element with odd occurrences. Note that XORing an element with itself results 0 and XOR of 0 with a number x is equal to x.
Try following complete program.

# include <iostream> using namespace std; int fun(int arr[], int n) { int x = arr[0]; for (int i = 1; i < n; i++) x = x ^ arr[i]; return x; } int main() { int arr[] = {9, 12, 2, 11, 10, 9, 12, 10, 9, 11, 2}; int n = sizeof(arr)/sizeof(arr[0]); cout << fun(arr, n) << endl; return 0; }

Question 2 |

What does the following C expression do?
x = (x<<1) + x + (x>>1);

Multiplies an integer with 7 | |

Multiplies an integer with 3.5 | |

Multiplies an integer with 3 | |

Multiplies an integer with 8 |

**Bit Algorithms**

**Discuss it**

Question 2 Explanation:

The expression multiplies an integer with 3.5. For example, if x is 4, the expression returns 15. If x is 6, it returns 21. If x is 5, it return 17. See Multiply a given Integer with 3.5 for more details.

Question 3 |

What does the following C expression do?

x = x & (x-1)

Sets all bits as 1 | |

Makes x equals to 0 | |

Turns of the rightmost set bit | |

Turns of the leftmost set bit |

**Bit Algorithms**

**Discuss it**

Question 3 Explanation:

The expression simply turns off the rightmost set bit. For example, if x = 14 (1110), x - 1 = 13 (1101) it returns resultant as (1100) i.e, 12 .

Question 4 |

Given two arrays of numbers a

_{1}, a_{2}, a_{3},...a_{n}and b_{1}, b_{2}, .. b_{n}where each number is 0 or 1, the fastest algorithm to find the largest span(i, j) such that a_{i}+ a_{i+1}, ....a_{j}= b_{i}+ b_{i+1}, .. b_{j}. or report that there is not such span,Takes O(n ^{3}) and Ω(2^{n}) time if hashing is permitted | |

Takes O(n ^{3}) and Ω(n^{2.5}) time in the key comparison model | |

Takes θ(n) time and space | |

Takes O(√n) time only if the sum of the 2n elements is an even number |

**Bit Algorithms**

**GATE-CS-2006**

**Discuss it**

Question 4 Explanation:

The problem can be solved in Takes θ(n) time and space.

**The idea is based on below observations.**- Since there are total n elements, maximum sum is n for both arrays.
- Difference between two sums varies from
**-n**to**n**. So there are total 2n + 1 possible values of difference. - If differences between prefix sums of two arrays become same at two points, then subarrays between these two points have same sum.

- Create an auxiliary array of size 2n+1 to store starting points of all possible values of differences (Note that possible values of differences vary from -n to n, i.e., there are total 2n+1 possible values)
- Initialize starting points of all differences as -1.
- Initialize
**maxLen**as 0 and prefix sums of both arrays as 0,**preSum1**= 0,**preSum2**= 0 - Travers both arrays from i = 0 to n-1.
- Update prefix sums: preSum1 += arr1[i], preSum2 += arr2[i]
- Compute difference of current prefix sums:
**curr_diff**= preSum1 – preSum2 - Find index in diff array:
**diffIndex**= n + curr_diff // curr_diff can be negative and can go till -n **If**curr_diff is 0, then i+1 is maxLen so far**Else If**curr_diff is seen first time, i.e., starting point of current diff is -1, then update starting point as i**Else**(curr_diff is NOT seen first time), then consider i as ending point and find length of current same sum span. If this length is more, then update maxLen

- Return maxLen

**See Longest Span with same Sum in two Binary arrays for complete running code**Question 5 |

Consider the following code snippet for checking whether a number is power of 2 or not.

/* Incorrect function to check if x is power of 2*/ bool isPowerOfTwo (unsigned int x) { return (!(x&(x-1))); }What is wrong with above function?

It does reverse of what is required | |

It works perfectly fine for all values of x. | |

It does not work for x = 0 | |

It does not work for x = 1 |

**Bit Algorithms**

**Discuss it**

Question 5 Explanation:

There are 5 questions to complete.