#!/perl -w
# NAME: bits.pl
# AIM: Testing bits
use strict;
use warnings;
my $showbit14 = 0;
my $value = 123;
my $bit1 = 1;
my $bit2 = 2;
my $bit3 = 4;
my $bit4 = 8;
show_val($value);
show_val(-1);
show_val(-2);
show_val(-3);
show_val(456);
exit(0);

sub prt {
    my ($txt) = shift;
    print $txt;
}

sub show_val {
    my ($v) = shift;
    if ($showbit14) {
        if ($v & $bit1) {
            prt("$v contains $bit1 ...\n" );
        } else {
            prt("$v DOES NOT contains $bit1 ...\n" );
        }
        if ($v & $bit2) {
            prt("$v contains $bit2 ...\n" );
        } else {
            prt("$v DOES NOT contains $bit2 ...\n" );
        }
        if ($v & $bit3) {
            prt("$v contains $bit3 ...\n" );
        } else {
            prt("$v DOES NOT contains $bit3 ...\n" );
        }
        if ($v & $bit4) {
            prt("$v contains $bit4 ...\n" );
        } else {
            prt("$v DOES NOT contains $bit4 ...\n" );
        }
    }
    my $bval = '';
    my $val = $v;
    my $cnt = 0;
    while ($val) {
        $cnt++;
        if ($val & 1) {
            $bval = '1'.$bval;
        } else {
            $bval = '0'.$bval;
        }
        $val = $val >> 1;
    }
    prt( "In binary, value $v is $bval ... and is $cnt bits wide\n" );
}

# eof
