Arithmetic Constants

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 6.  Performing Arithmetic


The format for arithmetic constants is:

number 
or
base#number 

where base is a whole number between 2 and 36, and number is any non-negative integer. If not specified, the default base is 10. The arithmetic base of a variable can be set with the typeset in command, or by prepending base# to the value. In this example, variable X is set to 5 in base 2 using both formats:

 $ typeset  i2 X=5 

or

 $ typeset  i X=2#101 

When variable X is expanded, the value is written in base 2:

 $ print $X  2#101 

If you want to display the value of X in another base, just reset the base with the typeset in command. Here it is reset to base 3:

 $ typeset  i3 X  $ print $X  3#12 

Arithmetic can be performed on numbers with different bases. Here is an example - X is set to 7 in base 2:

 $ typeset  i X=2#111 

Y is set to 8 in base 5:

 $ typeset  i5 Y=8 

and Z is set to base 16:

 $ typeset  i16 Z 

Now, X and Y are added together and the result is put in Z:

 $ Z=X+Y  $ print $Z  16#f 

We could convert the result to octal by resetting the base of Z using the typeset in command like this:

 $ typeset  i8 Z  $ print $Z  8#17 

       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net