Advertisement
exmkg

1-3

Jan 14th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. class Solution {
  2.     public int hammingWeight(int n) {
  3.         // Binary numbers are not really different
  4.         // from decimal numbers that we usually use.
  5.  
  6.         // In decimal representation (base 10),
  7.         // we have 10 digits (0, 1, 2, ..., 8, 9).
  8.         // If we start counting from 1, we have:
  9.         //    1 (which we can also write as 00001)
  10.         //    2 (which we can also write as 00002)
  11.         //    3 (which we can also write as 00003)
  12.         // ....
  13.         //    9 (which we can also write as 00009)
  14.         //   10 (which we can also write as 00010)
  15.         // ....
  16.         //   99 (which we can also write as 00099)
  17.         //  100 (which we can also write as 00100)
  18.         // ....
  19.         //  999 (which we can also write as 00999)
  20.         // 1000 (which we can also write as 01000)
  21.         // ....
  22.         // In decimal representation, if we want to
  23.         // add a 0 at the end of the number, we
  24.         // multiply it by 10 (since it's base 10 numbers).
  25.        
  26.         // In binary representation (base 2),
  27.         // we have just 2 digits (0 and 1).
  28.         // If we start counting from 1, we have:
  29.         //  1 is 00001 in binary representation.
  30.         //  2 is 00010 in binary repr.
  31.         //  3 is 00011 in binary repr.
  32.         //  4 is 00100 in binary repr.
  33.         //  5 is 00101 in binary repr.
  34.         //  6 is 00110 in binary repr.
  35.         //  7 is 00111 in binary repr.
  36.         //  8 is 01000 in binary repr.
  37.         //  9 is 01001 in binary repr.
  38.         // 10 is 01010 in binary repr.
  39.         // 11 is 01011 in binary repr.
  40.         // In binary representation, if we want to
  41.         // add a 0 at the end of the number, we
  42.         // multiply it by 2 (since it's base 2 numbers).
  43.  
  44.         // << is called a binary left-shift
  45.         // or just a left-shift operation.
  46.         // It is essentially adding a specified # of 0s
  47.         // at the end (in binary repr.),
  48.         // i.e. multiply by 2 specified number of times.
  49.  
  50.         int one  = 1 << 0; // binary repr: 00001
  51.         int two  = 1 << 1; // binary repr: 00010
  52.         int four = 1 << 2; // binary repr: 00100
  53.         // etc.
  54.  
  55.         // int is a 32-bit type, so it has bits 0-31.
  56.         // 31st bit is responsible for non-negative/negative notion.
  57.         // Since n is positive, we are only interested in bits
  58.         // 0-30.
  59.         // n = 11  00000000000000000000000001011 n
  60.         //         00000000000000000000000000001 bitmask
  61.         //         00000000000000000000000000001 n & bitmask
  62.  
  63.         // n = 11  00000000000000000000000001011 n
  64.         //         00000000000000000000000000010 bitmask
  65.         //         00000000000000000000000000010 n & bitmask
  66.  
  67.         // n = 11  00000000000000000000000001011 n
  68.         //         00000000000000000000000000100 bitmask
  69.         //         00000000000000000000000000000 n & bitmask
  70.  
  71.         // n = 11  00000000000000000000000001011 n
  72.         //         00000000000000000000000001000 bitmask
  73.         //         00000000000000000000000001000 n & bitmask
  74.        
  75.         int answer = 0;
  76.         for (int i = 0; i <= 30; i++) {
  77.             // i = 0, 000000000000001
  78.             // i = 1, 000000000000010
  79.             // i = 2, 000000000000100
  80.             // i = 3, 000000000001000
  81.             // ...
  82.             int bitmask = 1 << i;
  83.             if ((n & bitmask) > 0) {
  84.                 answer++;
  85.             }
  86.         }
  87.         return answer;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement