[Gllug] Unsigned shell arithmetic

tet at accucard.com tet at accucard.com
Tue Dec 4 09:15:04 UTC 2001


Anyone know how I can force unsigned arithmetic in a shell?

	echo $((65536 * 65000))

returns -35127296 both bash and pdksh, although genuine ksh on Solaris
returns 4259840000, which in this instance is what I want (fortunate,
as the script is primarily going to be used on Solaris). pdksh has a
-U option to typeset, which implies it'll do this, but it doesn't seem
to work. But I was more interested in whether or not it could be done
in Bourne shell, rather than bash, pdksh, zsh or any of the other
"enhanced" shells.

Also, does anyone know of a way to do bitwise operations in a standard
Bourne shell? I can do it in ksh or bash with $((a & b)), but at least
on Solaris, standard Bourne shell doesn't support that construct, and
neither does expr.

Any ideas?

Tet

PS.  Yes, I know I can do arbitrary precision unsigned arithmetic in dc,
     but dc doesn't have a bitwise AND operator, which unfortunately I need.

PPS. No comments about how I should be using perl, python or any other
     more appropiate tools. It's not an option for this particular task.
     The client has specified an unmolested Solaris 7 box, with no
     additional software installed.

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list