Mailing List Archive

Bitwise and
I have a problem with the following perl script:

#!/usr/local/bin/perl -w

$ip="213.234.124.46";
$mask = "255.255.255.0";
@mask=split ('\.', $mask);
@ip=split ('\.', $ip);
$x = $mask[0];
$y = $ip[0];
$z = $x & ($y);
$u = $x & ($y+0);
print ("x = $x, y = $y, x&y = $z ($u)\n");

Its output is:
Use of uninitialized value at ip.pl line 9.
x = 255, y = 213, x&y = 211 (213)
I expected $z to have the same value as $u.

myconfig tells me:
Summary of my perl5 (patchlevel 1) configuration:
Platform:
osname=linux, osver=1, archname=i486-linux
uname='linux slarti 1.2.11 #1 tue aug 29 14:04:15 gmt 1995 i486 '
hint=previous
Compiler:
cc='cc', optimize='-O2'
cppflags='-Dbool=char -DHAS_BOOL -g'
ccflags ='-Dbool=char -DHAS_BOOL -g -DDEBUGGING'
ldflags =''
stdchar='char', d_stdstdio=undef, usevfork=false
voidflags=15, castflags=0, d_casti32=undef, d_castneg=define
intsize=4, alignbytes=4, usemymalloc=n, randbits=31
Libraries:
so=sa
libpth=/lib /usr/lib /usr/local/lib
libs=-lgdbm -ldbm -ldb -lm -lc -lbsd
libc=/usr/lib/libc.sa
Dynamic Linking:
dlsrc=dl_none.xs, dlext=none, d_dlsymun=undef
cccdlflags='', ccdlflags='', lddlflags=''

Werner Wiethege

Deutsche Bank
Systemtechnik
Alfred-Herrhausen-Allee 16-24
D-65760 Eschborn
Germany

email: werner@dbna.com
Tel.: (+ 49) 69 910 69155
FAX: (+ 49) 69 910 69273

----------------------------------------------------------------------
Opinions are my own and not necessarily shared by my employer.
Re: Bitwise and [ In reply to ]
>I have a problem with the following perl script:

> #!/usr/local/bin/perl -w
> $ip="213.234.124.46";
> $mask = "255.255.255.0";
> @mask=split ('\.', $mask);
> @ip=split ('\.', $ip);
> $x = $mask[0];
> $y = $ip[0];
> $z = $x & ($y);
> $u = $x & ($y+0);
> print ("x = $x, y = $y, x&y = $z ($u)\n");


Are you understanding string versus numeric anding? Here's a good
article on the matter... but I suppose I'd better more strongly
document it.

Date: 07 Nov 1995 06:18:01 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: '&' operator on strings
To: h.kim@intelsat.int
Reply-to: merlyn@stonehenge.com
In-reply-to: h.kim@intelsat.int's message of Tue, 7 Nov 1995 00:53:52
Organization: Stonehenge Consulting Services; Portland, Oregon, USA
Newsgroups: comp.lang.perl.misc
Article 10877 of comp.lang.perl.misc:
X-Newsreader: Gnus v5.0.12


>>>>> "Hong" == Hong S Kim <h.kim@intelsat.int> writes:

Hong> Hi,
Hong> I have installed perl5.001m on our HP715 box and the installation
Hong> went smoothly.

Hong> My colleague was trying an example from "Teach Yourself Perl in 21
Hong> Days" which states that strings are the treated the same as
Hong> numbers. Specifically that both

Hong> 1. $answer = 124.3 & 99;
Hong> 2. $answer = "124.3" & "99";

Hong> should yield 96. Number (2) however gives the value 10. Why?

Unfortunately, there are two ways to think of bit operators on strings:

(1) interpret the strings as numbers, convert the numbers to integers,
perform the operation, and then re-float that back to a number, or
(2) interpret the strings as *bit patterns*, and perform the bit operation
directly on strings.

For many moons (perl 2 to perl 4), Perl used interpretation #1
*unless* there was a vec() present in the program, and then it used
interpretation #2. In an effort to eliminate "unexpected action at a
distance" from perl 5, Larry made the bit-ops *always* use
interpretation #2. If you want to bit-and the numeric value of the strings,
just make them a number yourself:

$answer = (0+"124.3") & (0+"99");

Sure, it's a little more hassle, but well worth the elimination of
"action at a distance".

Yes, this means that:

print "01" & "1";

will print 1 in perl4 (in absence of a vec), and 0 in perl5. Oh well.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
# legal fund: $14811.84 collected, $129659.85 spent; email fund@stonehenge.com
for details--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me