Unix Tip: Using bc for big calculations

March 12, 2008, 05:44 PM —  ITworld — 

For sysadmins who want to perform simple calculations on the command line, there's a considerably more useful tool than expr. Try bc. Like expr, the bc command allows you to do simple math. Here are some examples:

boson> echo 6*6-3 | bc
33
boson> amt=`echo 6*6-3 | bc`
boson> echo $amt
33

You can also go into calculator mode by typing bc on the command line by itself and then feed it as many calculations as you want:

boson% bc
7*7*7
343
256*256
65536
256*256*256
16777216
256*256*256*256
4294967296
256*256*256*256*256*256*256*256
18446744073709551616
quit

Notice that you can calculate some very large values! You can also achieve a high degree of precision by setting the number of places you want to see following the decimal point.

boson% bc
scale=4
177/7
25.2857
quit

The "scale" setting, as you can see from the example above, determines the number of decimal places to save. You can, therefore, get considerably more useful values than the rounded down results of expr. Using bc, you don't have to escape multiplication operations and computing powers of 2 (or any arbitrary number for that matter) is simple, fast and accurate as shown below.

boson% expr 3 \* 3
9
boson% echo 3*3 | bc
9
boson% bc
2^8
256
2^32
4294967296
quit

Look how quickly we can calculate the number of IP addresses that will be available in IPv6 as opposed to IPv4:

boson% bc
2^32
4294967296
2^128
340282366920938463463374607431768211456
quit

That's one staggering large number, but bc can generate even larger numbers with ease and speed:

boson% bc
2^256
11579208923731619542357098500868790785326998466564056403945758400791\
3129639936
2^512
13407807929942597099574024998205846127479365820592393377723561443721\
76403007354697680187429816690342769003185818648605085375388281194656\
9946433649006084096
2^1024
17976931348623159077293051907890247336179769789423065727343008115773\
26758055009631327084773224075360211201138798713933576587897688144166\
22492847430639474124377767893424865485276302219601246094119453082952\
08500576883815068234246288147391311054082723716335051068458629823994\
7245938479716304835356329624224137216
quit

The numbers wrap around in 68-digit segments until the complete number has been printed on the screen. The bc command is also useful for doing base conversions. Say you want to know the hexadecimal equivalent of 255. Just do this:

boson% bc
obase=16
255
FF
quit

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Close

On Twitter now

Unix

Powered by Twitter
You are logged in | Sign out
Sign in and post to Twitter

What are you thinking?

Cancel Tweet sent

On Twitter now

Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace