<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: KSH Script Questikon in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513499#M897607</link>
    <description>Hi Chris,&lt;BR /&gt;&lt;BR /&gt;You prette much came with your answer yourself.&lt;BR /&gt;&lt;BR /&gt;When using typeset -i you create a signed integer of 32 bit, giving it a range from:&lt;BR /&gt;&lt;BR /&gt;-2147483648 to 2147483648&lt;BR /&gt;&lt;BR /&gt;My numbers are not 100% accurate, but what happends is that if you have an integer of the size 2147483648, adding one, will turn on the last bit in the integer, giving a signed value of:&lt;BR /&gt;-2147483648&lt;BR /&gt;&lt;BR /&gt;adding one to this will increase this number by one etc.&lt;BR /&gt;&lt;BR /&gt;So it's simply an integer overflow you are experiencing :)&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Uffe</description>
    <pubDate>Wed, 04 Apr 2001 14:56:29 GMT</pubDate>
    <dc:creator>Uffe Gavnholt_1</dc:creator>
    <dc:date>2001-04-04T14:56:29Z</dc:date>
    <item>
      <title>KSH Script Questikon</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513498#M897606</link>
      <description>OK all you KSH experts out there.  I have an interesting one, but one I know that someone will know the answer to right away.  How is this possible?  Here is the text of a test script I wrote:&lt;BR /&gt;&lt;BR /&gt;#!/sbin/ksh&lt;BR /&gt;&lt;BR /&gt;typeset -i a&lt;BR /&gt;typeset -i b&lt;BR /&gt;typeset -i c&lt;BR /&gt;typeset -i d&lt;BR /&gt;typeset -i e&lt;BR /&gt;typeset -i f&lt;BR /&gt;typeset -i g&lt;BR /&gt;typeset -i h&lt;BR /&gt;&lt;BR /&gt;((a=159))&lt;BR /&gt;((b=256))&lt;BR /&gt;((c=256*256))&lt;BR /&gt;((d=256*256*256))&lt;BR /&gt;((e=$a*$b))&lt;BR /&gt;((f=$a*$c))&lt;BR /&gt;((g=$a*$d))&lt;BR /&gt;((h=$a*$b*$c))&lt;BR /&gt;&lt;BR /&gt;echo "a = $a"&lt;BR /&gt;echo "b = $b"&lt;BR /&gt;echo "c = $c"&lt;BR /&gt;echo "d = $d"&lt;BR /&gt;echo "e = $e"&lt;BR /&gt;echo "f = $f"&lt;BR /&gt;echo "g = $g"&lt;BR /&gt;echo "h = $h"&lt;BR /&gt;&lt;BR /&gt;So when I run the script, here are my results:&lt;BR /&gt;&lt;BR /&gt;a = 159&lt;BR /&gt;b = 256&lt;BR /&gt;c = 65536&lt;BR /&gt;d = 16777216&lt;BR /&gt;e = 40704&lt;BR /&gt;f = 10420224&lt;BR /&gt;g = -1627389952&lt;BR /&gt;h = -1627389952&lt;BR /&gt;&lt;BR /&gt;Now, I am a pretty smart guy, so I know that multiplying or adding 2 positive numbers is supposed to give you a positive number in return.  Why am I getting negative values for g and h?  I was told it may have something to do with using 16-bit number representations or something along those lines.  Any ideas out there?  Thanks for the help!&lt;BR /&gt;&lt;BR /&gt;Chris Ivey&lt;BR /&gt;&lt;BR /&gt;HP OpenView Certified Consultant - NNM 6.x on Unix (0010-01-42093)&lt;BR /&gt;MTS - Systems Engineer&lt;BR /&gt;Verizon Data Services, ENSS-West WAN Tools Group&lt;BR /&gt;Office: (813) 978-4844&lt;BR /&gt;Pager: (813) 303-1177 &lt;BR /&gt;ICQ: 25717924&lt;BR /&gt;AIM: IveyAtGTEDS</description>
      <pubDate>Wed, 04 Apr 2001 14:29:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513498#M897606</guid>
      <dc:creator>Chris Ivey</dc:creator>
      <dc:date>2001-04-04T14:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: KSH Script Questikon</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513499#M897607</link>
      <description>Hi Chris,&lt;BR /&gt;&lt;BR /&gt;You prette much came with your answer yourself.&lt;BR /&gt;&lt;BR /&gt;When using typeset -i you create a signed integer of 32 bit, giving it a range from:&lt;BR /&gt;&lt;BR /&gt;-2147483648 to 2147483648&lt;BR /&gt;&lt;BR /&gt;My numbers are not 100% accurate, but what happends is that if you have an integer of the size 2147483648, adding one, will turn on the last bit in the integer, giving a signed value of:&lt;BR /&gt;-2147483648&lt;BR /&gt;&lt;BR /&gt;adding one to this will increase this number by one etc.&lt;BR /&gt;&lt;BR /&gt;So it's simply an integer overflow you are experiencing :)&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Uffe</description>
      <pubDate>Wed, 04 Apr 2001 14:56:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513499#M897607</guid>
      <dc:creator>Uffe Gavnholt_1</dc:creator>
      <dc:date>2001-04-04T14:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: KSH Script Questikon</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513500#M897608</link>
      <description>Hi Chris,&lt;BR /&gt;&lt;BR /&gt;Negative output is normal, considering the fact that your variables are 32-bit signed integers (i.e. -2^31 &amp;lt;= integer value &amp;lt;= 2^31 - 1). So when your operation is giving a positive result greater than 2147483647 (2^31 - 1), output will be negative.&lt;BR /&gt;&lt;BR /&gt;You can see this behaviour in ksh with the following commands:&lt;BR /&gt;&lt;BR /&gt;--snip--&lt;BR /&gt;&amp;gt; typeset -i a&lt;BR /&gt;&amp;gt; a=2147483647&lt;BR /&gt;&amp;gt; echo $a&lt;BR /&gt;2147483647&lt;BR /&gt;&amp;gt; ((a=a+1))&lt;BR /&gt;&amp;gt; echo $a&lt;BR /&gt;-2147483648&lt;BR /&gt;--snip--&lt;BR /&gt;&lt;BR /&gt;If you want positive results instead, you could e.g. use the following workaround:&lt;BR /&gt;&lt;BR /&gt;--snip--&lt;BR /&gt;&amp;gt; a=2147483647&lt;BR /&gt;&amp;gt; echo "scale=2;$a+1" | bc&lt;BR /&gt;2147483648&lt;BR /&gt;--snip--&lt;BR /&gt;&lt;BR /&gt;I hope this helps.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;BR /&gt;&lt;BR /&gt;Fred.</description>
      <pubDate>Wed, 04 Apr 2001 15:07:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513500#M897608</guid>
      <dc:creator>Frederic Soriano</dc:creator>
      <dc:date>2001-04-04T15:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: KSH Script Questikon</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513501#M897609</link>
      <description>Chris,&lt;BR /&gt;&lt;BR /&gt;This is quite normal.  When doing arithmetic in a shell, I prefer to pass the arguments to an awk statement, e.g.&lt;BR /&gt;&lt;BR /&gt;echo 159 256 |&lt;BR /&gt;awk '{ a = $1&lt;BR /&gt;           b = $2&lt;BR /&gt;           c = $2 * $2&lt;BR /&gt;           d = $2 * $2 * $2&lt;BR /&gt;           e = a * b&lt;BR /&gt;           f = a * c&lt;BR /&gt;           g = a * d&lt;BR /&gt;           h = a * b * c&lt;BR /&gt;           print a&lt;BR /&gt;           print b&lt;BR /&gt;           print c&lt;BR /&gt;           print d&lt;BR /&gt;           print e&lt;BR /&gt;           print f&lt;BR /&gt;           printf("%ld\n", g)&lt;BR /&gt;           printf("%ld\n", h) }'&lt;BR /&gt;&lt;BR /&gt;This should give you the correct results.&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;&lt;BR /&gt;Joseph.</description>
      <pubDate>Sun, 08 Apr 2001 22:53:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-script-questikon/m-p/2513501#M897609</guid>
      <dc:creator>Joseph A Benaiah_1</dc:creator>
      <dc:date>2001-04-08T22:53:51Z</dc:date>
    </item>
  </channel>
</rss>

