bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bug: convert decimal to binary


From: Chet Ramey
Subject: Re: bug: convert decimal to binary
Date: Tue, 15 Feb 2005 09:25:54 -0500

> Hi,
> 
> is it a bug in bash?!
> 
> to convert great numbers: binary to decimal
> 
> # decimal do binary
> $ echo "obase=2;9876543212345678909876" | bc -l
> 10000101110110100010010010001010110111010110110011100111000101000110\
> 110100
> 
> # binary to decimal
> $ echo "$((2#10000101110110100010010010001010110111010110110011100111\
> 000101000110110100))"
> 7535132911068795316

You're hitting integer overflow.  If you were to try $(( 9876543212345678909876 
))
you'd get the same result.  The arithmetic expansion and arithmetic evaluation
code doesn't check for overflow.

Bash uses intmax_t (usually 64-bit) arithmetic where it can.  987654321234567890
is the largest portion of the number you supplied that can be represented in
an intmax_t without overflow.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live...Laugh...Love
Chet Ramey, ITS, CWRU    chet@case.edu    http://tiswww.tis.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]