Samss77
Samss77

Reputation: 15

Perl remove digits from a binary number

I want to remove few digits from a 24-bit binary number and convert it to 18-bit binary number.

For example:

if Binary number

bin24=111100111011111000100111

I want to remove bits 23:22, 15:14 and 7:6 and the output should be

bin18=110011111110100111

I know I can do this using substr() and concatenate. Just wanted to know if I can do this in one line?.

Upvotes: 1

Views: 104

Answers (2)

mob
mob

Reputation: 118625

$bin18 = join '', unpack('x2 a6 x2 a6 x2 a6', $bin24);

Upvotes: 8

Sinan Ünür
Sinan Ünür

Reputation: 118148

my $bin18 = reverse join '', (reverse ($bin24 =~ /([01])/g))[0 .. 5, 8 .. 13, 16 ..21];

and other variations on the theme.

Upvotes: 1

Related Questions