satveer singh
satveer singh

Reputation: 13

expr command in tcl 8.4.9 version treats result as 32 bit signed integer only

I am running Tcl 8.4.9. I'm dealing with 64-bit address and need to perform some arithmetic operations on 64-bit address. I used the expr command but it returns a negative result. I do not want to upgrade Tcl version is there any other alternative for it??

set addr 0xffff00001000000
set offset 0x01

set newaddr [expr {$addr + $offset}]

if {$newaddr < 0} {
    puts "Less than ZERO"
}

how to overcome such issues is there any other command to do arithmetic operations?

Upvotes: 0

Views: 244

Answers (1)

Shawn
Shawn

Reputation: 52449

The math::bignum library from tcllib is listed as having a minimum version requirement of 8.4. So you should be able to use it (Though updating to 8.6 gives you a lot more bonuses than being able to use large integers).

Example (Using an interactive tclsh repl session):

% package require math::bignum
3.1.1
% set addr [::math::bignum::fromstr 0xffff00001000000]
bignum 0 0 256 61440 4095
% set offset [::math::bignum::fromstr 0x01]
bignum 0 1
% set newaddr [::math::bignum::add $addr $offset]
bignum 0 1 256 61440 4095
% puts [::math::bignum::tostr $newaddr 16]
ffff00001000001

Compared to tcl 8.6 native math:

% set addr 0xffff00001000000
0xffff00001000000
% set offset 0x01
0x01
% set newaddr [expr {$addr + $offset}]
1152903912437579777
% puts [format %x $newaddr]
ffff00001000001

Same non-negative result.

Upvotes: 1

Related Questions