Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Hi

I have a simple code like below but the results do not make sense. Perhaps, I am doing something wrong:


#!/usr/bin/perl use strict; use warnings; my ($A, $B) = ""; $A = 0b0; $B = 0b0; my $z = (~$A & ~$B); print "Z: "; printf "%b\n", $z; print "Z: "; printf "%d\n", $z;

If we run the above code, I get the following results:
Z: 1111111111111111111111111111111111111111111111111111111111111111 Z: -1

But in reality,  $z should be a binary value of  1 as  ~$A & ~$B = ~0b0 & ~0b0 = 0b1 & 0b1 = 0b1 <bd> Please help me know what I am doing wrong
Thanks.

Replies are listed 'Best First'.
Re: Boolean calculation incorrect values
by Athanasius (Archbishop) on Dec 24, 2015 at 08:05 UTC

    The Perl ~ operator performs bitwise negation. As you say you’re expecting a result of 1 (and not -1), I think you’re looking for logical negation:

    17:58 >perl -wE "printf '%b', (~0b0 & ~0b0);" 1111111111111111111111111111111111111111111111111111111111111111 18:00 >perl -wE "printf '%b', (!0b0 & !0b0);" 1 18:01 >

    See perlop#Symbolic-Unary-Operators.

    Update: Added the underlined clause.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The preferred term is bitwise complement. Most computer arithmetic uses two's complement to represent numbers so the complement and the negation of a bit pattern are not the same. Because the two's complement negation of a number is equivalent to the one's complement negation plus 1, the two's complement negation of a single bit number is the same as the original number!

      Which all seems a whole lot more pedantic than I really intended, but the notion that negating a number leaves it unchanged was just too neat to pass up :-D.

      Premature optimization is the root of all job security

        ++ for two useful and important points — 1’s vs. 2’s complement, and negation leaving a number unchanged (!) — but I have to disagree about the terminology. “Bitwise complement” is ambiguous for precisely the reasons you give, unless “1’s complement” is explicitly specified. But “bitwise negation” is unambiguous, and is the term used in the official documentation (perlop#Symbolic-Unary-Operators):

        Unary "~" performs bitwise negation, that is, 1's complement. For example, 0666 & ~027 is 0640. ... Note that the width of the result is platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 bits wide on a 64-bit platform, so if you are expecting a certain bit width, remember to use the "&" operator to mask off the excess bits.

        The Camel Book refers to ~ as “bitwise NOT” (4th Edition, p. 118).

        Happy Christmas,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Boolean calculation incorrect values
by GrandFather (Saint) on Dec 24, 2015 at 08:10 UTC

    About the only thing you are doing wrong is over complicating your sample code. Consider:

    #!/usr/bin/perl use strict; use warnings; my $z = ~0; printf "%b 0x%x %d\n", $z, $z, $z;

    Prints:

    1111111111111111111111111111111111111111111111111111111111111111 0xff +ffffffffffffff -1

    which is a 64 bit value with all bits set. That is the twos compliment representation of -1 and we've printed it as a binary value, a hex value and as a decimal value.

    ~ performs a bitwise compliment of the contents of a variable so the result is just what we expect. None of the subsequent operations you performed in your sample code changed the bit pattern.

    Premature optimization is the root of all job security