in reply to Making all nibble zero lower than the offset

#!/usr/bin/env perl use strict; use warnings; my $data = 0b1111_1111_1111; my $offset = 8; my $nibble_mask = 0xF; while (not 1<<$offset-1 & $nibble_mask) { $data ^= $nibble_mask; $nibble_mask <<= 4; } $data = $data ^ $nibble_mask; printf("%b\n",$data);

Replies are listed 'Best First'.
Re^2: Making all nibble zero lower than the offset
by trippledubs (Deacon) on Nov 20, 2018 at 18:46 UTC
    How naive I was to loop through looking for something already given...
    #!/usr/bin/env perl use strict; use warnings; my $data = 0b1111_1111_1111; my $offset = 5; my $mask = hex 'F' x ($offset % 4 == 0 ? $offset / 4 : $offset / 4 + 1 +); printf("%b\n",$data ^ $mask);