473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Perl Math Question


An odd (to me) behavior I was hoping someone could explain.

Take the simple command:

perl -e 'print 0xCEC5 + 0xFFFFE253 & 0xFFFF'

I tried this on a number of computers. Most give 0xFFFF, which just
seems wrong. This includes Perl 5.6 and 5.8 on Linux and Darwin (OSX).
Also one Solaris box with 5.0. One computer (Perl 5.6, HPUX) gave what
I thought to be the correct answer (0xB118). Is this some sort of type
conversion issue that trips up the 32-bit PowerPCs and x86s but the
64-bit HPUX avoids?

Interestingly,

perl -e 'print 0xCEC5 + 0xFFFFE253 & 0xBEEF'

0xBEEF

Neat trick. Totally wrong answer, though.

perl -e 'print (0xCEC5 + 0xFFFFE253) & 0xFFFF'

gives 0x10000B118, which I don't understand.

perl -e 'print 0xCEC5 + (0xFFFFE253 & 0xFFFF)'

gives 0x1B118, which makes sense.

Obviously, I'm not grokking the perl bitwise-and (&) operator. The FAQ
warned about accidently anding strings, but stuff like

print oct("0xCEC5") + oct("0xFFFFE253") & oct("0xFFFF")

also gets the wrong answer, so I don't think that's my problem.

Any enlightenment appreciated.

CP

--
'When religion and politics ride in the same cart, the whirlwind follows.'
Jul 19 '05 #1
3 5563
Chris Pruett <sp**@pruett.org> wrote in message news:<150920032356168229%sp**@pruett.org>...
<snip> perl -e 'print (0xCEC5 + 0xFFFFE253) & 0xFFFF'

gives 0x10000B118, which I don't understand.

<snip>

This is hexadecimal arithmetic (not maths ;-> ) and the answer is
correct.

Simply add 0xCEC5 to 0xFFFFE253 and the result is 0x10000B118.

Anding 0xFFFF with 0x10000B118, leaves the least significant four
digits alone
i.e. in binary, bitwise anding 1010 with 1111 and you get 1010.

Perl does not appear to left fill 0xFFFF with leading zeroes so you do
not lose the five most significant digits of the addition result. I
would hesitate to say that perl is left filling with 1's on an and
operation; rather that it performs the bitwise operation on as many
bits as are appropriate; in this example the least significant four.

HTH

Lesley
Jul 19 '05 #2
> >perl -e 'print (0xCEC5 + 0xFFFFE253) & 0xFFFF'
gives 0x10000B118, which I don't understand.

This is hexadecimal arithmetic (not maths ;-> ) and the answer is
correct.


However, perl -we will give a warning, which may help the understanding.
The &0xFFFF is applied to the return value of the print.
Jul 19 '05 #3
In article <3F***************@world.std.com>, Bill N1VUX
<wd*@world.std.com> wrote:
perl -e 'print (0xCEC5 + 0xFFFFE253) & 0xFFFF'
gives 0x10000B118, which I don't understand.

This is hexadecimal arithmetic (not maths ;-> ) and the answer is
correct.


However, perl -we will give a warning, which may help the understanding.
The &0xFFFF is applied to the return value of the print.


Thanks for the reply.

Interesting point about the return value of the print. That seems to
explain how I get 0x10000B118 from

perl -e 'print (0xCEC5 + 0xFFFFE253) & 0xFFFF'

I'm still on my quest to get the right answer though (0xB118).

$x = (0xCEC5 + 0xFFFFE253);
print "$x\n";
$x &= 0xFFFF;
print "$x\n"'

4295012632 [0x10000B118]
65535 [0xFFFF]

That's still not the answer I expect.

In the $x &= 0xFFFF, does Perl try to convert the 0x10000B118 to a
native integer type (32-bit), detect the overflow, and saturate the
result (0xFFFFFFFF) before applying the bitwise-and? That would seem
to explain some of the results I'm getting and it might explain why
the 64-bit HPUX version gets the correct number.
CP

--
'When religion and politics ride in the same cart, the whirlwind follows.'
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: @ | last post by:
A benchmark in 2002 showed PHP is much slower in shell or when Apache has Mod_Perl. With the new PHP kissing Java's ass, Perl is once again the #1 CGI choice. Java is for a big team in short...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
2
by: TSO | last post by:
Hi there, I've recently tried to translate some Perl code into Python - code is below. Is there a more Pythonic form?
6
by: WipeOut | last post by:
I am having a problem in a perl script that I can't seem to find an answer for.. The $cost and $retail vars come from another part of the script and would be something like 000134.345 and...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
13
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
3
by: haochen89 | last post by:
Hi, When I do this in Perl: if ((35/26) == (0.35/0.26)) { print "equal\n"; } else { print "not equal\n"; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.