Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have a below piece of code:

$tempVar = hex(get_data());

$test |= $tempVar << (64 - $shiftby * ($size - $iteration - 1));

This code is to be executed on perl 5.26. Currently it is executing on 5.16.

When i try to execute this on 5.26, it gives random results (Mostly 0 everytime). It works fine on 5.16.

I needed some help on how to handle this on 5.26. Basically the below equation should be modified to run on both 5.16 and 5.26:
$test |= $tempVar << (64 - $shiftby * ($size - $iteration - 1));

Any help/guidance would be appreciated.

Thanks

What I have tried:

I tried the below:

1) Went through the perl config operations which are used to build perl 5.26 and perl 5.16.

2) Perl 5.26 is built with "
Quote:
-Duse64bitint
". I think due to this it explicitly treats all variables as 64 bits. However Perl 5.16 is not built with this.

3) I am trying to build perl 5.26 without this flag. But this will be cumbersome as we might move to some other version in near future. I am not sure if this is the right approach.

4) Finding alternative of this for example:
$tempVar |= $tempVar << (64 - $shiftby * ($size - $iteration - 1))
$test |= $tempVar

This however gives some defined value but with some delta.
Posted

1 solution

At a guess, you're experiencing a difference in how negative shifts are performed. Given
PERL
$t = 256;
$v = $t << -1;
print ("v = $v\n");

Running this on perl 5.16 (CentOS 7), I get 0, but using perl 5.36 (Debian bookworm), I get a value of 128.
If 64 - $shiftby * ($size -$iteration -1) is negative, you'll need to decide what you should do. Maybe right-shift instead? Abort? other?
 
Share this answer
 
Comments
Rahul VB 15-May-24 5:09am    
Thanks for the quick response. But, i dont get negetive values. I am exploring more.
k5054 15-May-24 12:16pm    
Only thing I can suggest is that you build up your expression a bit at a time and examine the values as you go e.g.
$x = ($size - iteration -1);
$y = $shiftby * $x;
$z = 64 - $shiftby;
$val  = $TestVal << $z;
$test |= $val;
print STDERR, "x = $x, y = $y, z = $z, val = $val, test = $test\n';

At least then, you might get an idea where the two versions diverge.
Rahul VB 22-May-24 12:30pm    
Thanks, for the response. I had to correct the shift logic which was implemented after breaking the code further down.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900