Source code is onb = 1 << (i + 3); So if i is ever >= 28, overflow occurs. Suggest either sanity check i before use, or use the following code: onb = 1UL << (i + 3); and move type of variable onb to unsigned long.