check out my comment ...i had posted it log time back on geekforgeeks...search for atull007 int the below link. let me know it does not work http://www.geeksforgeeks.org/forum/topic/adobe-interview-question-for-software-engineerdeveloper-fresher-about-bit-magic-1
On Thu, Nov 1, 2012 at 4:00 AM, rahul sharma <[email protected]>wrote: > You are given two 32-bit numbers, N and M, and two bit positions, i and j. > Write a method to set all bits between i and j in N equal to M (e.g., M > becomes a substring of N located at i and starting at j). > EXAMPLE: > Input: N = 10000000000, M = 10101, i = 2, j = 6 > Output: N = 10001010100 > > Following is code...guys i think that in line no. 5 (1<<j) this should > be replaced with (1<<j+1).means we have to shift 1 left by j+1 bits..Line > no. 5 should be > int left = max - ((1 << j+1) - 1); > plz comment ??? > > public static int updateBits(int n, int m, int i, int j) { > 2 int max = ~0; /* All 1’s */ > 3 > 4 // 1’s through position j, then 0’s > 5 int left = max - ((1 << j) - 1); > 6 > 7 // 1’s after position i > 8 int right = ((1 << i) - 1); > 9 > 10 // 1’s, with 0s between i and j > 11 int mask = left | right; > 12 > 13 // Clear i through j, then put m in there > 14 return (n & mask) | (m << i); > 15 } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
