CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
"bits and bits" !~ /scambled eggs for brain/;

 



john
Deleted

Apr 20, 2000, 12:52 PM

Post #1 of 10 (5691 views)
"bits and bits" !~ /scambled eggs for brain/; Can't Post

Here's the code:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach (sort n (keys %bkmenu)) {
my $thiskey = $_;
print "Raw key: $thiskey\t";
$thiskey = "0" x ($l - (length $thiskey)) . $thiskey;
print "Padded key : $thiskey\n";
my $dthiskey = $menu->b2d($thiskey);
my $truth = $binary & $thiskey;
print "The TRUTH is $truth\t ($binary and $thiskey )\n";
if ($truth != 0) {
push @ret, $dthiskey;
}
}
</pre><HR></BLOCKQUOTE>

The & produces the expected result MOST of the time. I cannot determine why I get output like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>
sorting keys and comparing ...
Raw key: 0 Padded key : 00000
The TRUTH is 0 ( 00110 and 00000 )
Raw key: 1 Padded key : 00001
The TRUTH is 0 ( 00110 and 00001 )
Raw key: 10 Padded key : 00010
The TRUTH is 10 ( 00110 and 00010 )
Raw key: 100 Padded key : 00100
The TRUTH is 100 ( 00110 and 00100 )
Raw key: 1000 Padded key : 01000
The TRUTH is 104 ( 00110 and 01000 )
Raw key: 10000 Padded key : 10000
The TRUTH is 0 ( 00110 and 10000 )
<HR></BLOCKQUOTE>

Am I not understanding the bitwise '&' ???

TIA
John


Cure
User

Apr 20, 2000, 2:25 PM

Post #2 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

Hi

perldoc perlop
gives info about bitwise operators


Cure


Cure
User

Apr 20, 2000, 3:47 PM

Post #3 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

Hi

You seemed to be doing a *stringwise* -> but the code is doing a *bitwise* and the results happen purely by chance!
the $truth is being printed as an integer, not as a five-digit string.

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

the 104 becomes 00000 -> are five-digit strings.


Cure


john
Deleted

Apr 20, 2000, 3:49 PM

Post #4 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>
Binary ``&'' returns its operators ANDed together bit by bit.
<HR></BLOCKQUOTE>

... got it.

but how does "00110" & "01000" = 104 ??? ...
"The TRUTH is 104 ( 00110 and 01000 )"

I don't grok that if I treat them as strings or as binary numbers. Also, why does only one iteration through the loop do this, while the other iterations seem to evaluate as (I) expected?

My mind is boggled...must be cause I learned old math not new math. <g>

John


john
Deleted

Apr 20, 2000, 4:38 PM

Post #5 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

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.

John


Cure
User

Apr 20, 2000, 4:49 PM

Post #6 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

Hi


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.



Cure
User

Apr 20, 2000, 4:52 PM

Post #7 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

Why do you want to use that method anyway?

I personally have never used that method-> so I'm kinda rusty on it.

What kind of program are you trying to achieve here -> so, maybe we can approach it better way.

Cure


john
Deleted

Apr 20, 2000, 5:13 PM

Post #8 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

As I mentioned, you have answered my original question, Thank You.

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.)

Thanks again,
John


john
Deleted

Apr 20, 2000, 5:59 PM

Post #9 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

What I really meant to do is this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub amath {
my ($self, $svalue, $menuid) = @_;
if (wantarray) {
my @ret;
my $l = @{$self->{$menuid}}-1;
my $toobig = 2**$l;
if ($svalue >= $toobig) {
return "error, paymth number out of range";
}
my $m;
for ($m = 0; $m <= $l; $m++) {
my $n = 2**$m;
my $dcompare = $n & $svalue;
push @ret, $dcompare unless ($dcompare == 0);
}
return @ret;
} else {
# take the array and return the sum of its elements
my $return;
no strict 'refs';
map({$return += $_} @{$svalue});
use strict;
return $return;
}
}
</pre><HR></BLOCKQUOTE>

As usual, the GURUS have been a great help in prodding my somewhat buffudled brain in the right direction, Thanks!

John


Cure
User

Apr 21, 2000, 11:03 AM

Post #10 of 10 (5691 views)
Re: "bits and bits" !~ /scambled eggs for brain/; [In reply to] Can't Post

Your Welcome John

Bye, the way-> incase you didnt know-> But im sure you know.


if (wantarray) {
return @array;
}
else {
return $string;
}


you can say all that with one line of code

return wantarray ? @array : $string;

Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives