The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-15-2007
Perderabo's Avatar
Perderabo Perderabo is offline
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,718
If the course says that ((~ 2#1001)) evaluates to 2#110, I wonder what shell they were using. With ksh I get:
$ typeset -i2 b
$ echo $((2#1001)) $((b=~2#1001)) $b
9 -10 -2#1010

With bash, there is no -i2 and I don't see an easy way to get binary output. I could code my own routine, but then the behavior would be whatever I decided. (But I get the "9 -10" part with bash.) Since ksh alone has a way to output binary, I did a few experiments with it. A negative number has many leading 1's while a non-negative has many leading zeros. ksh uses enough positions to output the the leftmost bit transistion. If you exceed the shell's integer size, you get undefined results, but clamping at positive or negative "infinity" (max/min integer) is common with both ksh and bash. The shells I checked have either 32 or 64 bit integers.
Reply With Quote