sweet1cris

Untitled

Jan 9th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1.  
  2. class Solution {
  3.     /**
  4.      *@param n, m: Two integer
  5.      *@param i, j: Two bit positions
  6.      *return: An integer
  7.      */
  8.     public int updateBits(int n, int m, int i, int j) {
  9.         // write your code here
  10.         int max = ~0; /* All 1’s */
  11.         // 1’s through position j, then 0’s
  12.         if (j == 31)
  13.             j = max;
  14.         else
  15.             j = (1 << (j + 1)) - 1;
  16.         int left = max - j;
  17.         // 1’s after position i
  18.         int right = ((1 << i) - 1);
  19.         // 1’s, with 0s between i and j
  20.         int mask = left | right;
  21.         // Clear i through j, then put m in there
  22.         return ((n & mask) | (m << i));
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment