Here you can find the source of BitsNeeded(int n)
public static byte BitsNeeded(int n)
//package com.java2s; //License from project: Open Source License public class Main { public static byte BitsNeeded(int n) { byte ret = 1; if (n-- == 0) return 0; while ((n >>= 1) != 0) ++ret;//from ww w .j ava2 s .c om return ret; } }