The only thing that's wrong is that you didn't account for leading zero's.
Your initial question has a type who's size is always 1 byte.
However your second question, the one where 'something is wrong' requires
more bits of information to hold the given value.
You need to modify the sprintf to pad the binary number with the leading
zeros  (Or you can look at Tom's module which I haven't done).

my $alignment = 8;
my $u = 0x4D4F;
my $padding = $alignment + $u.msb - ( $u.msb +& ( $alignment  - 1 ) );
say '$u = <0b' ~ "\%0{$padding}b".sprintf($u).comb(/\d ** 4/).join('_') ~
'>;';


On Tue, Feb 4, 2020 at 2:04 AM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> >> On Mon, Feb 3, 2020 at 8:17 PM ToddAndMargo via perl6-users
> >> <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:
> >>
> >>     On 2020-02-03 13:51, ToddAndMargo via perl6-users wrote:
> >>      > p6 'my uint8 $u = 84; printf "\$u = <%08s>\n", $u;'
> >>
> >>     Oops, that should have been
> >>
> >>     $ p6 'my uint8 $u = 0x4F; printf "\$u = <%#b>\n", $u;'
> >>     $u = <0b1001111>
> >>
> >>     Who do I get it to print
> >>
> >>           0b0100_1111
> >>
> >>     ?
> >>
> >>     -T
> >>
>
> On 2020-02-03 20:28, Paul Procacci wrote:
> > Here's one way....
> >
> > my uint8 $u = 0x4F;
> > say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d ** 4/).join('_') ~ '>;';
> >
> >
> > There's probably others as well.
>
> Hi Paul,
>
> Something is wrong.  Dang!
>
> -T
>
>
>  > my $u = 0x4D4F; say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d **
> 4/).join('_') ~ '>;';
> $u = <0b1001_1010_1001>;
>
>  > say 0b1001_1010_1001.base(16)
> 9A9
>
>
>  > my $u = 0x3D4F; say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d **
> 4/).join('_') ~ '>;';
> $u = <0b1111_0101_0011>;
>
>  > say 0b1111_0101_0011.base(16)
> F53
>


-- 
__________________

:(){ :|:& };:

Reply via email to