Both change it into int and adding bound check doesn't work for me. But this work for me: index &= 134217727u; One limitation is 134217727u has to be power of 2 minus 1.