in reply to Re^3: Decimal to Binary using Bitwise and operator
in thread Decimal to Binary using Bitwise and operator

print "Input a decimal number lesser than 256: ", "\n"; my $decimal; $decimal = <STDIN>; print "The decimal number AND 128 gives us: ", ($decimal & 128)>1, "\n +"; print "The decimal number AND 64 gives us: ", ($decimal & 64)>1, "\n"; print "The decimal number AND 32 gives us: ", ($decimal & 32)>1, "\n"; print "The decimal number AND 16 gives us: ", ($decimal & 16)>1, "\n"; print "The decimal number AND 8 gives us: ", ($decimal & 8)>1, "\n"; print "The decimal number AND 4 gives us: ", ($decimal & 4)>1, "\n"; print "The decimal number AND 2 gives us: ", ($decimal & 2)>1, "\n\n"; print "The decimal number AND 1 gives us: ", $decimal & 1, "\n";

2017-09-02 Athanasius added code tags

Replies are listed 'Best First'.
Re^5: Decimal to Binary using Bitwise and operator
by roboticus (Chancellor) on Sep 02, 2017 at 17:25 UTC

    Anonymous Monk:

    You can save yourself some cut & paste if you do it like:

    $ cat pm_1198573.pl #!env perl use strict; use warnings; print "Input a decimal number between 0 and 255: "; my $decimal = <STDIN> + 0; die "Please follow directions!" if $decimal < 0 or $decimal > 255; print "The number $decimal AND ", (1<<$_), " gives us: ", ($decimal & +1<<$_), "\n" for reverse 0 .. 7; $ perl pm_1198573.pl Input a decimal number between 0 and 255: 113 The number 113 AND 128 gives us: 0 The number 113 AND 64 gives us: 64 The number 113 AND 32 gives us: 32 The number 113 AND 16 gives us: 16 The number 113 AND 8 gives us: 0 The number 113 AND 4 gives us: 0 The number 113 AND 2 gives us: 0 The number 113 AND 1 gives us: 1

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.