Sponsored Content
Top Forums Shell Programming and Scripting How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H Post 302669405 by rrd1986 on Wednesday 11th of July 2012 02:30:34 AM
Old 07-11-2012
How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi,
Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result.

Code:
perl interpreter info,
perl, v5.8.8 built for i386-linux-thread-multi

Program snippet,

Code:
use bignum qw/hex/;

$x ="SAD_MISS_REGION:Base:Size
SAD_MISS_REGION:0x100000000:0x100000000
SAD_MISS_REGION:0x400000000:0x400000000
SAD_MISS_REGION:0x1800000000:0x3FFE7FFFFFFFF";

while ( $x =~ /SAD_MISS_REGION:(.+):(.+)/g ) {

$p = $1; $q = $2;
$phex = hex($1); $qhex = hex($2);

}

print "\n\n\n\nBase: $p, Size: $q \n";
print "\n\n\n\nBaseinDecimal: $phex, SizeinDesimal: $qhex \n";

$z = $phex + $qhex;
print "Z has summed up value:  $z\n";

$zhex = unpack "H*", $z;
#$zhex = uc(sprintf("%x\n", $z));
print "Hexadecimal number of Z: ", $zhex, "\n";

output:

Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF




BaseinDecimal: 103079215104, SizeinDesimal: 1125796827627519
Z has summed up value:  1125899906842623
Hexadecimal number of Z: 31313235383939393036383432363233

------------------------------------------------------------

Math::BigInt seems to be a solution but i dont have it installed at my remote machine. Need a work around. Please help....


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 03:34 AM.. Reason: code tags, see PM. Should know that with 18 posts...
 

4 More Discussions You Might Find Interesting

1. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

2. Red Hat

can not reboot : PCI: Unable to handle 64-bit address space for

Hello, every one, I tried to install redhat linux enterprise server version 4 (ES4) on the my system with the following configuration Pentium core 2 duo 2.66 E 6750 Intel DG33FB motherboard 160Gb Segate hard disk (SATA) 1024 mb (1GB) DDR2 TRAN RAM Lg DVD/Cd WR BUT AM GETTING... (0 Replies)
Discussion started by: moinkhan31
0 Replies

3. Red Hat

boot the 32 bit kernel on a 64 bit PPC Linux machine?

Hi all, I'm looking to cover a corner case for an upcoming test cycle. Is there a way to boot a RedHat Advanced Server 4 (update 3) installed on a Power PC machine to use a 32 bit kernel? This would be similar to what is done here -> https://www.unix.com/aix/26204-aix-platform.html I've done... (0 Replies)
Discussion started by: philrau
0 Replies

4. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies
math::bignum(n) 						 Tcl Math Library						   math::bignum(n)

__________________________________________________________________________________________________________________________________________________

NAME
math::bignum - Arbitrary precision integer numbers SYNOPSIS
package require Tcl ?8.4? package require math::bignum ?3.1? ::math::bignum::fromstr string ?radix? ::math::bignum::tostr bignum ?radix? ::math::bignum::sign bignum ::math::bignum::abs bignum ::math::bignum::cmp a b ::math::bignum::iszero bignum ::math::bignum::lt a b ::math::bignum::le a b ::math::bignum::gt a b ::math::bignum::ge a b ::math::bignum::eq a b ::math::bignum::ne a b ::math::bignum::isodd bignum ::math::bignum::iseven bignum ::math::bignum::add a b ::math::bignum::sub a b ::math::bignum::mul a b ::math::bignum::divqr a b ::math::bignum::div a b ::math::bignum::rem a b ::math::bignum::mod n m ::math::bignum::pow base exp ::math::bignum::powm base exp m ::math::bignum::sqrt bignum ::math::bignum::rand bits ::math::bignum::lshift bignum bits ::math::bignum::rshift bignum bits ::math::bignum::bitand a b ::math::bignum::bitor a b ::math::bignum::bitxor a b ::math::bignum::setbit bignumVar bit ::math::bignum::clearbit bignumVar bit ::math::bignum::testbit bignum bit ::math::bignum::bits bignum _________________________________________________________________ DESCRIPTION
The bignum package provides arbitrary precision integer math (also known as "big numbers") capabilities to the Tcl language. Big numbers are internally represented at Tcl lists: this package provides a set of procedures operating against the internal representation in order to: o perform math operations o convert bignums from the internal representation to a string in the desired radix and vice versa. But the two constants "0" and "1" are automatically converted to the internal representation, in order to easily compare a number to zero, or increment a big number. The bignum interface is opaque, so operations on bignums that are not returned by procedures in this package (but created by hand) may lead to unspecified behaviours. It's safe to treat bignums as pure values, so there is no need to free a bignum, or to duplicate it via a spe- cial operation. EXAMPLES
This section shows some simple example. This library being just a way to perform math operations, examples may be the simplest way to learn how to work with it. Consult the API section of this man page for information about individual procedures. package require math::bignum # Multiplication of two bignums set a [::math::bignum::fromstr 88888881111111] set b [::math::bignum::fromstr 22222220000000] set c [::math::bignum::mul $a $b] puts [::math::bignum::tostr $c] ; # => will output 1975308271604953086420000000 set c [::math::bignum::sqrt $c] puts [::math::bignum::tostr $c] ; # => will output 44444440277777 # From/To string conversion in different radix set a [::math::bignum::fromstr 1100010101010111001001111010111 2] puts [::math::bignum::tostr $a 16] ; # => will output 62ab93d7 # Factorial example proc fact n { # fromstr is not needed for 0 and 1 set z 1 for {set i 2} {$i <= $n} {incr i} { set z [::math::bignum::mul $z [::math::bignum::fromstr $i]] } return $z } puts [::math::bignum::tostr [fact 100]] API
::math::bignum::fromstr string ?radix? Convert string into a bignum. If radix is omitted or zero, the string is interpreted in hex if prefixed with 0x, in octal if pre- fixed with ox, in binary if it's pefixed with bx, as a number in radix 10 otherwise. If instead the radix argument is specified in the range 2-36, the string is interpreted in the given radix. Please note that this conversion is not needed for two constants : 0 and 1. (see the example) ::math::bignum::tostr bignum ?radix? Convert bignum into a string representing the number in the specified radix. If radix is omitted, the default is 10. ::math::bignum::sign bignum Return the sign of the bignum. The procedure returns 0 if the number is positive, 1 if it's negative. ::math::bignum::abs bignum Return the absolute value of the bignum. ::math::bignum::cmp a b Compare the two bignums a and b, returning 0 if a == b, 1 if a > b, and -1 if a < b. ::math::bignum::iszero bignum Return true if bignum value is zero, otherwise false is returned. ::math::bignum::lt a b Return true if a < b, otherwise false is returned. ::math::bignum::le a b Return true if a <= b, otherwise false is returned. ::math::bignum::gt a b Return true if a > b, otherwise false is returned. ::math::bignum::ge a b Return true if a >= b, otherwise false is returned. ::math::bignum::eq a b Return true if a == b, otherwise false is returned. ::math::bignum::ne a b Return true if a != b, otherwise false is returned. ::math::bignum::isodd bignum Return true if bignum is odd. ::math::bignum::iseven bignum Return true if bignum is even. ::math::bignum::add a b Return the sum of the two bignums a and b. ::math::bignum::sub a b Return the difference of the two bignums a and b. ::math::bignum::mul a b Return the product of the two bignums a and b. The implementation uses Karatsuba multiplication if both the numbers are bigger than a given threshold, otherwise the direct algorith is used. ::math::bignum::divqr a b Return a two-elements list containing as first element the quotient of the division between the two bignums a and b, and the remain- der of the division as second element. ::math::bignum::div a b Return the quotient of the division between the two bignums a and b. ::math::bignum::rem a b Return the remainder of the division between the two bignums a and b. ::math::bignum::mod n m Return n modulo m. This operation is called modular reduction. ::math::bignum::pow base exp Return base raised to the exponent exp. ::math::bignum::powm base exp m Return base raised to the exponent exp, modulo m. This function is often used in the field of cryptography. ::math::bignum::sqrt bignum Return the integer part of the square root of bignum ::math::bignum::rand bits Return a random number of at most bits bits. The returned number is internally generated using Tcl's expr rand() function and is not suitable where an unguessable and cryptographically secure random number is needed. ::math::bignum::lshift bignum bits Return the result of left shifting bignum's binary representation of bits positions on the left. This is equivalent to multiplying by 2^bits but much faster. ::math::bignum::rshift bignum bits Return the result of right shifting bignum's binary representation of bits positions on the right. This is equivalent to dividing by 2^bits but much faster. ::math::bignum::bitand a b Return the result of doing a bitwise AND operation on a and b. The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ::math::bignum::bitor a b Return the result of doing a bitwise OR operation on a and b. The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ::math::bignum::bitxor a b Return the result of doing a bitwise XOR operation on a and b. The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ::math::bignum::setbit bignumVar bit Set the bit at bit position to 1 in the bignum stored in the variable bignumVar. Bit 0 is the least significant. ::math::bignum::clearbit bignumVar bit Set the bit at bit position to 0 in the bignum stored in the variable bignumVar. Bit 0 is the least significant. ::math::bignum::testbit bignum bit Return true if the bit at the bit position of bignum is on, otherwise false is returned. If bit is out of range, it is considered as set to zero. ::math::bignum::bits bignum Return the number of bits needed to represent bignum in radix 2. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: bignum of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
bignums, math, multiprecision, tcl COPYRIGHT
Copyright (c) 2004 Salvatore Sanfilippo <antirez at invece dot org> Copyright (c) 2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net> math 3.1 math::bignum(n)
All times are GMT -4. The time now is 03:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy