I'm trying to understand part of this code that was created here to create a my_hashset: https://leetcode.com/problems/design-hashset/discuss/548792/Java-solution-faster-than-99
class HashCode_from_leet {
int[] bitArr;
private static final int MAX = 100000;
private static final int INT_SIZE = Integer.SIZE;
public HashCode_from_leet() {
bitArr = new int[MAX / INT_SIZE + 1];
}
public void add(int key) { //pass in 5
int ind = key / INT_SIZE;
int bit = key % INT_SIZE;
int bitMask = 1 << bit; //QUESTION: why does bitMask = 32?
bitArr[ind] |= bitMask; // QUESTION: what does '|=' mean?
}
// NOTE I REMOVED SOME METHODS
public boolean contains(int key) {
int ind = key / INT_SIZE;
int bit = key % INT_SIZE;
int bitMask = 1 << bit;
return (bitArr[ind] & bitMask) != 0;
}
public static void main(String[] args) {
HashCode_from_leet hfl = new HashCode_from_leet();
hfl.add(5);
System.out.println(hfl.contains(5));
}
}
When I pass in 5 into the add(int key)
method bitMask = 32
I'm not sure why -- I understand <<
means shift left, so we take 5 and shift it left, but would think that equals 5*10^2?
When you shift left
an integer by X eg: Z =(Y << X)
it will acutally do this operation: Z = (Y * (2^X))
When you shift right
an integer by X eg: Z =(Y >> X)
it will acutally do this operation: Z = (Y / (2^X))
what happens when you shift an int left
As the name suggests, left-shifting a number by n
places, multiplies it by 2^n
. The following code will help you understand it better.
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println("In decimal: " + x + ", In Binary: "
+ "0000000000000000".substring(Integer.toBinaryString(x).length()) + Integer.toBinaryString(x));
int y = x << 1;
System.out.println("In decimal: " + y + ", In Binary: "
+ "0000000000000000".substring(Integer.toBinaryString(y).length()) + Integer.toBinaryString(y));
int z = x << 2;
System.out.println("In decimal: " + z + ", In Binary: "
+ "0000000000000000".substring(Integer.toBinaryString(z).length()) + Integer.toBinaryString(z));
}
}
Output:
In decimal: 5, In Binary: 0000000000000101
In decimal: 10, In Binary: 0000000000001010
In decimal: 20, In Binary: 0000000000010100
Notice line int bit = key % INT_SIZE; // which is 5 % 32 according to your code
int bit = key % INT_SIZE; // which is 5 % 32 according to your code
So now bit = 5 // because 5 % 32 = 5
Now int bitMask = 1 << bit; // you're saying shit 1 to five bits to left
int bitMask = 1 << bit; // you're saying shit 1 to five bits to left
in 32 bits 1 = 000000000000000000000000000000000001
After shifting 5 bits to left it becomes:
000000000000000000000000000000000100000 = 32 //in binary that is why bitmask continas 32
Now let's talk about |=
operator
|
is a bitwise OR operator you are just using as compound assignment
bitArr[ind] |= bitMask is similar to bitArr[ind] = bitArr[ind] | bitmask
For example say bitArr[ind] equals 5 and bitmask equals 3
now bitArr[ind] | bitmask
bitArr[ind] | bitmask
means 5 | 3
5 | 3
It will do bitwise OR with binary representation
means 101 | 011
101 | 011
which becomes 111
which is 7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.