File: math.rst

package info (click to toggle)
bashacks 1.5%2Bgit20241128.a8d16fc-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: sh: 435; python: 34; makefile: 32
file content (185 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Math
====

In this section, we will have many funcions of mathematics operations that help us in everyday life.

They are not complex to assemble in bash, however, nothing better than a small function to assist, nothing better that send data and get or return result, always read the code.


bh_bin2dec
----------

This function expects a binary and return its equivalent in decimal.

.. note::
    
    Usage

    ``bh_bin2dec`` [binary]

    binary   :   string in binary format.


.. code-block:: bash

    $ bh_bin2dec 11111111
    255

    $ bh_bin2dec 10
    2

    $ bh_bin2dec 1110
    14


bh_charcalc
-----------

Think of a way to make operations with 'char', how to sum two positions for a 'char/string' and return letter c or sum of the other and multiply it by 10 and returns 10 

.. note::
    
    Usage

    ``bh_charcalc`` [char/string] [operator] [number]

    char/string   :   string or char to operation 
    operator :  \* + -
    number :  num of operation


.. code-block:: bash

    $ bh_charcalc A + 2
    C

    $ bh_charcalc A \* 255
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.......


bh_dec2bin
----------

Opossed to ``bh_bin2dec`` this function expects a decimal for converting it into binary.


.. note:: 

    Usage

    ``bh_dec2bin`` [decimal]

    decimal :  number in decimal format.


.. code-block:: bash

    $ bh_dec2bin 255
    11111111

    $ bh_dec2bin 2
    10

    $ bh_dec2bin 14
    1110

    #Example 

    $ for dec in {1..6};
     do
        echo "$dec = $(bh_dec2bin $dec)";
     done 

    1 = 1
    2 = 10
    3 = 11
    4 = 100
    5 = 101
    6 = 110


bh_dec2hex
----------

The function expects a input a decimal number it performs the conversion to hex.

.. note:: 

    Usage

    ``bh_dec2hex`` [decimal]

    decimal: number in decimal format


.. code-block:: bash

    $ bh_dec2hex 10
    a

    $ bh_dec2hex 255
    ff


bh_hex2bin
----------

Capture all submitted arguments and convert to binary

.. note:: 

    Usage

    ``bh_hex2bin`` [list or one hex digit]


.. code-block:: bash

    $ bh_hex2bin abcdef 1 2 3
    101010111100110111101111 1 10 11

    $ bh_hex2bin 10
    10000


bh_hex2dec
----------

This's a conversion function from hex digit to decimal digit

.. note:: 

    Usage

    ``bh_hex2dec`` [one or more hex digit]


.. code-block:: bash

    $ bh_hex2dec A
    10

    $ bh_hex2dec FF
    255


bh_hexcalc
----------

In the same way as ``bh_charcalc``, however, work here with hexdigit.

.. note:: 

    Usage

    ``bh_hex2cal`` [hex digit] [operator] [hex digit]


.. code-block:: bash

    $ bh_hex2dec A \* 2
    0xa0

    $ bh_hex2bin FF + 1
    0x100