where mask is a.....mask.... with ones on the places of replacement
and offset the ....offset... where to start with replacing?
(did not think proper of the logic. Maybe ones or zeros get shifted in. Or if some more brackets are needed. Just meant as an idea... Not the full solution!)