How to resolve the algorithm Binary digits step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Binary digits step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Create and display the sequence of binary digits for a given   non-negative integer. The results can be achieved using built-in radix functions within the language   (if these are available),   or alternatively a user defined function can be used. The output produced should consist just of the binary digits of each number followed by a   newline. There should be no other whitespace, radix or sign markers in the produced output, and leading zeros should not appear in the results.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Binary digits step by step in the UNIX Shell programming language

Source code in the unix programming language

bin() {
    set -- "${1:-0}" ""
    while [ 1 -lt "$1" ]
    do
        set -- $(($1 >> 1)) $(($1 & 1))$2
    done
    echo "$1$2"
}

echo $(for i in 0 1 2 5 50 9000; do bin $i; done)

  

You may also check:How to resolve the algorithm Combinations step by step in the Egison programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the PL/I programming language
You may also check:How to resolve the algorithm String case step by step in the BQN programming language
You may also check:How to resolve the algorithm Gray code step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Bracmat programming language