public void write(int value, int numBits) {
        if (numBits <= 0) return;
        byte[] bits = new byte[numBits];
        for (int i = 0; i < numBits; i++) {
            bits[i] = (byte) (value & 0x0001);
            value >>= 1;
        }
        for (int i = numBits - 1; i >= 0; i--) {
            int b = bits[i] << (7 - bit);
            buf[index] |= b;
            bit++;
            if (bit > 7) {
                bit = 0;
                index++;
                if (index >= buf.length) {
                    // buffer is full; increase the size
                    byte[] newBuf = new byte[buf.length * 2];
                    System.arraycopy(buf, 0, newBuf, 0, buf.length);
                    buf = newBuf;
                }
            }
        }
    }
Writes the given value using the given number of bits.
mesh/src/main/java/no/nordicsemi/android/mesh/utils/BitWriter.java