# Primitive Type

## HashCode

How String hashCode is calculated?

```
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
```

[Reference](https://www.geeksforgeeks.org/how-string-hashcode-value-is-calculated/)

## Parity

Giving a binary word, if the number of 1s is odd, return 1, otherwise 0

* (11010111) => (1101)^(0111)

```java
public static short parity(long x) {
    x ^= (x >>> 32);
    x ^= (x >>> 16);
    x ^= (x >>> 8);
    x ^= (x >>> 4);
    x ^= (x >>> 2);
    x ^= (x >>> 1);
    return (short) (x & 1);
  }
```

## 50 Compute x^y

```java
 private static double power(double x, int y){
    double result= 1.0;
    boolean isPositive = true;
    if (y < 0){
        isPositive = false;
        y = - y;
    }
    while( y > 0){
        if ( y%2 == 1){
            result *= x;
        }
        x *= x;
        y /= 2;
    }
    if(!isPositive){
        result = 1 / result;
    }
    return result;
}
```
