This is a question from Laakman. 

This code operates by clearing all bits in N between position i and j, and then 
ORing to put M in there 
1 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 }


Gaurav


On Oct 1, 2011, at 4:26 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
> _
> 
> #include<stdio.h>
> #include<stdlib.h>
> int main()
> {
>     int N,M,i,j;
>     printf("Enter value  of N \n");
>     scanf("%d",&N);
>     fflush(stdin);
>     printf("Enter value  of M \n");
>     scanf("%d",&M);
>     fflush(stdin);
>     printf("Enter value  of i \n");
>     scanf("%d",&i);
>     fflush(stdin);
>     printf("Enter value  of j \n");
>     scanf("%d",&j);
>     fflush(stdin);
>     int a=0,k;
>     for( k=0;k<j;k++)
>     {
>               a= a<<1;
>               a=a|1;
>     }
>     for(k =0;k<i;k++)
>     {
>           a=a<<1;
>     }
> 
>     N = N &(~a);
>     printf("value of N is %d",N);
>     for(k=0;k<i;k++)
>     M=M<<1;
>     N=N|M;
>     printf("value of N is %d",N);
>     getchar();
> }
> 
> isnt it give us wrong mask????
> say i=2;
> j=6;
> it gives mask as(i.e ~a)
> 1111111100000011
> but i think from 2 to 6 5 0's are needed????plz tell the above prog is 
> ok???or not???check by giving any input whose 7thy bit is set...thnx in 
> advance
> -- 
> 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.

Reply via email to