how to do this with bitmasking

question link how to do this question with the help of bitmasking.I saw some people’s submission and saw some binary operation which i could not understand if anybody has done this,please explain in great detail.Assume me beginner in bitmanipulation