Binary, octal, and hex arithmetic operations in Linux shell

April 09, 2021

Integer values can be represented in decimal, binary, octal, or hex numeric notations. By default, integer values are represented in decimal notation. Binary numbers have base 2. Octal numbers use base 8. Hexadecimal numbers use base 16. We will learn about various notations with examples in this section.

This is the syntax:

variable=base#number-in-that-base

Let’s understand the preceding syntax with examples:

  • Decimal representation:
$ declare -i x=21
$ echo $x21
  • Binary representation:
$ x=2#10101
$ echo $x21
  • Octal representation:
$ declare -i x
$ x=8#25
$ echo $x21
  • Hexadecimal representation:
$ declare -i x
$ x=16#15
$ echo $x21

In the preceding examples, we displayed the decimal 21 value in binary, octal, and hexadecimal representations.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

16 + 14 =