in reply to Boolean calculation incorrect values

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,

Replies are listed 'Best First'.
Re^2: Boolean calculation incorrect values
by GrandFather (Saint) on Dec 24, 2015 at 08:28 UTC

    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,