solution using bitwise operations