$a&$b == $a & ~(~$b)Contrapositive law $a&$b == $a & ~ (-1-$b)Extended definition of ~ Since -1-$b is positive, $a & ~(-1-$b) can be evaluated in bitwise fashion.

$a<0,$b>=0

Commute to ($b & $a) and use the calculation above

$a<0,$b<0

$a&$b == ~ (~$a | ~$b)De Morgan's Law $a&$b == ~ ((-1-$a) | (-1-$b))Extended definition of ~ $a&$b == -1-((-1-$a) | (-1-$b))Extended definition of ~ Since -1-$a and -1-$b are both positive, the expression ((-1-$a) | (-1-$b)) can be evaluated in the ordinary bitwise fashion.

AMG: Be wary of the precedence of this operator! Like &&, it has lower precedence than the comparison operators. For more information on why, read the section "Neonatal C" found at [1 ].

I want to convert signed integer to 32-bit hexadecimal.

The following works fine for positive integer:

format %.8x $the_decimal_value

But for negative integer, I got 64-bit hexadecimal.

For example 99 is converted to 00000063, but -81 is converted to ffffffffffffffaf.

Anyone know how to get 32-bit hexadecimal fro negative integer? Thanks in advance!

AMG: The %.8x means to pad to eight characters only if narrower than eight to begin with. You want to truncate the value. What you're looking for is the & operator. Try this:

% format %.8x [expr {$the_decimal_value & 0xffffffff}]