Sorry for the late reply.
I was looking for a better approach to the problem rather than bit masking and bitwise operator.

I am finding it , as soon as i find it I will post it.
Thank you shabbir and dawei