ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT

Unix Tip: Using bc for big calculations

ITworld 3/12/2008

Sandra Henry-Stocker, ITworld.com

Bookmark and Share

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
Maybe that's too easy. Let's try some larger numbers:

boson% bc
obase=16
4294967296
100000000
340282366920938463463374607431768211456
100000000000000000000000000000000
quit
No surprise there, right? Since we fed bc the decimal values of 2^32 and 2^128, we would expect a number starting with 1 and followed by a long string of zeroes. If we want to see the largest values (in hex) that we can hold in 32 and 128 bits, we can do this:

boson% bc
obase=16
4294967296-1
FFFFFFFF
340282366920938463463374607431768211456-1
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
quit
We can also feed arbitrarily long numbers into bc using the same continuation line form that bc uses to display long numbers:

boson% bc
obase=16
13407807929942597099574024998205846127479365820592393377723561443721\
76403007354697680187429816690342769003185818648605085375388281194656\
9946433649006084096
1000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000
quit
Other numeric bases can be selected as well. In the example below, we're electing to use octal and then ... septal?

boson% bc
obase=8
12
14
obase=7
8
11
quit
While bc has tremendous functionality for quick math, you probably don't want to use it for every bit of math you do in every script you write since many languages have math skills of their own. On the other hand, bc will beat the socks off most desktop calculators and it's fun to get some expert help with those base conversions.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

IP Networks Boost Secure Health Communications
AT&T provides secure communication to keep health care moving forward.
Understanding VPN Technology Choices
Knowing the VPN options can help a manager work more effectively with available technologies.
IMPROVE YOUR SUPPORT EFFICIENCY
WebEx lets you remotely control, configure and install applications and updates more efficiently.
Intel® Technology’s Impact On Mobile IT Efficiency
Check Out How Centrino® With vPro™ Technology Is Cost Effectively Keeping PC Fleets Productive.
Improving the View with IP Videoconferencing
New videoconferencing technologies are poised to benefit the enterprise.
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
 Home   Computers and Peripherals  Computing systems  Servers  Enterprise  Unix enterprise servers
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.