DOH! <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR> [john@melanie perl]# ./anotherbintest.pl 62 1 AND 62 is 0 2 AND 62 is 2 4 AND 62 is 4 8 AND 62 is 8 16 AND 62 is 16 32 AND 62 is 32 64 AND 62 is 0 <HR></BLOCKQUOTE> I guess all that coversion to something that looked like a binary number to me was not a good idea...

Thanks for the help, I kept strugling to understand what you were saying... and then it dawned on me... computers always think things are binary numbers.... bitwise anything ...oh yea... now i get it ... sort of.

You go out of your way to produce five-digit strings for $thiskey, and $binary is a five-digit string, so you seem to be doing a *stringwise* AND. But the code is doing a *bitwise* AND, and the result happens -- purely by chance! -- to look like the result of a stringwise AND, except for the case of '01000'. The clue is that $truth is being printed as an integer, not as a five-digit string.

When I stringify $thiskey explicitly (usefully double-quoting a simple variable!):

my $truth = $binary + "$thiskey";

the 104 becomes 00000 (and all the other TRUTHs are five-digit strings, as expected).

The reason I am baffled is this:

print "Padded key : $thiskey\n"; # In the line above, $thiskey is a five-digit string. my $truth = $binary & $thiskey; # In the line above, $binary is a five-digit string. print "The TRUTH is $truth\t ($binary and $thiskey )\n"; # In the line above, $thiskey is a five-digit string.

Why does $thiskey become an integer, which 'integrifies' the value of $binary, for the duration of the conjuction statement, but is still a string after it???

In binary, 00110 is 0001101110 01000 is 1111101000

The bitwise AND is 0001101000 which is 104

Because if you do the same conversions and bitwise arithmetic as I did above, you will get BY PURE COINCIDENCE a binary result which, when converted to decimal, looks the same as a string-wise AND.

I'm writing a sub routine that will accept either a scalar and return an array, or accept an array and return a scalar.

If I pass the subroutine (method) an array, I expect it to return the sum of its elements. (That works.)

However where (wantarray) I want the method to take the supplied scalar, and return the bitwise AND of an array of integers which are consecutive ordered exponents of two.

I'm working with a vary simple "bit mapped" data elements where a stored value of 3 means "option 1 and option 2", a stored value of 9 means "option 1 and option 8". See what I mean? (I know what I'm trying to do, I'm just explaining it really poorly.)