File: Bit-Manipulations.html

package info (click to toggle)
octave 10.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (258 lines) | stat: -rw-r--r-- 15,163 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Bit Manipulations (GNU Octave (version 10.3.0))</title>

<meta name="description" content="Bit Manipulations (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Bit Manipulations (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Numeric-Data-Types.html" rel="up" title="Numeric Data Types">
<link href="Logical-Values.html" rel="next" title="Logical Values">
<link href="Integer-Data-Types.html" rel="prev" title="Integer Data Types">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="section-level-extent" id="Bit-Manipulations">
<div class="nav-panel">
<p>
Next: <a href="Logical-Values.html" accesskey="n" rel="next">Logical Values</a>, Previous: <a href="Integer-Data-Types.html" accesskey="p" rel="prev">Integer Data Types</a>, Up: <a href="Numeric-Data-Types.html" accesskey="u" rel="up">Numeric Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Bit-Manipulations-1"><span>4.5 Bit Manipulations<a class="copiable-link" href="#Bit-Manipulations-1"> &para;</a></span></h3>

<p>Octave provides a number of functions for the manipulation of numeric
values on a bit by bit basis.  The basic functions to set and obtain the
values of individual bits are <code class="code">bitset</code> and <code class="code">bitget</code>.
</p>
<a class="anchor" id="XREFbitset"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitset"><span><code class="def-type"><var class="var">B</var> =</code> <strong class="def-name">bitset</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">n</var>)</code><a class="copiable-link" href="#index-bitset"> &para;</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-bitset-1"><span><code class="def-type"><var class="var">B</var> =</code> <strong class="def-name">bitset</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">n</var>, <var class="var">val</var>)</code><a class="copiable-link" href="#index-bitset-1"> &para;</a></span></dt>
<dd><p>Set or reset bit(s) at position <var class="var">n</var> of the unsigned integers in <var class="var">A</var>.
</p>
<p>The least significant bit is <var class="var">n</var> = 1.  <var class="var">val</var>&nbsp;=&nbsp;0<!-- /@w -->&nbsp;resets bits
and <var class="var">val</var>&nbsp;=&nbsp;1<!-- /@w -->&nbsp;sets bits.  If no <var class="var">val</var> is specified it
defaults to 1 (set bit).  All inputs must be the same size or scalars.
</p>
<p>Example 1: Set multiple bits
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">x = bitset (1, 3:5)
  &rArr; x =

   5    9   17

dec2bin (x)
  &rArr;
     00101
     01001
     10001
</pre></div></div>

<p>Example 2: Reset and set bits
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">x = bitset ([15 14], 1, [0 1])
  &rArr; x =

   14    15
</pre></div></div>

<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitand">bitand</a>, <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<a class="anchor" id="XREFbitget"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitget"><span><code class="def-type"><var class="var">b</var> =</code> <strong class="def-name">bitget</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">n</var>)</code><a class="copiable-link" href="#index-bitget"> &para;</a></span></dt>
<dd><p>Return the bit value at position(s) <var class="var">n</var> of the unsigned integers in
<var class="var">A</var>.
</p>
<p>The least significant bit is <var class="var">n</var> = 1.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bitget (100, 8:-1:1)
&rArr; 0  1  1  0  0  1  0  0
</pre></div></div>

<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitand">bitand</a>, <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<p>The arguments to all of Octave&rsquo;s bitwise operations can be scalar or
arrays, except for <code class="code">bitcmp</code>, whose <var class="var">k</var> argument must a
scalar.  In the case where more than one argument is an array, then all
arguments must have the same shape, and the bitwise operator is applied
to each of the elements of the argument individually.  If at least one
argument is a scalar and one an array, then the scalar argument is
duplicated.  Therefore
</p>
<div class="example">
<pre class="example-preformatted">bitget (100, 8:-1:1)
</pre></div>

<p>is the same as
</p>
<div class="example">
<pre class="example-preformatted">bitget (100 * ones (1, 8), 8:-1:1)
</pre></div>

<p>It should be noted that all values passed to the bit manipulation
functions of Octave are treated as integers.  Therefore, even though the
example for <code class="code">bitset</code> above passes the floating point value
<code class="code">10</code>, it is treated as the bits <code class="code">[1, 0, 1, 0]</code> rather than the
bits of the native floating point format representation of <code class="code">10</code>.
</p>
<p>As the maximum value that can be represented by a number is important
for bit manipulation, particularly when forming masks, Octave supplies
two utility functions: <code class="code">flintmax</code> for floating point integers, and
<code class="code">intmax</code> for integer objects (<code class="code">uint8</code>, <code class="code">int64</code>, etc.).
</p>
<p>Octave also includes the basic bitwise &rsquo;and&rsquo;, &rsquo;or&rsquo;, and &rsquo;exclusive or&rsquo;
operators.
</p>
<a class="anchor" id="XREFbitand"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitand"><span><code class="def-type"><var class="var">z</var> =</code> <strong class="def-name">bitand</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">y</var>)</code><a class="copiable-link" href="#index-bitand"> &para;</a></span></dt>
<dd><p>Return the bitwise AND of non-negative integers.
</p>
<p><var class="var">x</var>, <var class="var">y</var> must be in the range [0,intmax]
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<a class="anchor" id="XREFbitor"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitor"><span><code class="def-type"><var class="var">z</var> =</code> <strong class="def-name">bitor</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">y</var>)</code><a class="copiable-link" href="#index-bitor"> &para;</a></span></dt>
<dd><p>Return the bitwise OR of non-negative integers <var class="var">x</var> and <var class="var">y</var>.
</p>

<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<a class="anchor" id="XREFbitxor"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitxor"><span><code class="def-type"><var class="var">z</var> =</code> <strong class="def-name">bitxor</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">y</var>)</code><a class="copiable-link" href="#index-bitxor"> &para;</a></span></dt>
<dd><p>Return the bitwise XOR of non-negative integers <var class="var">x</var> and <var class="var">y</var>.
</p>

<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitand">bitand</a>, <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<p>The bitwise &rsquo;not&rsquo; operator is a unary operator that performs a logical
negation of each of the bits of the value.  For this to make sense, the
mask against which the value is negated must be defined.  Octave&rsquo;s
bitwise &rsquo;not&rsquo; operator is <code class="code">bitcmp</code>.
</p>
<a class="anchor" id="XREFbitcmp"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitcmp"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">bitcmp</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">k</var>)</code><a class="copiable-link" href="#index-bitcmp"> &para;</a></span></dt>
<dd><p>Return the <var class="var">k</var>-bit complement of integers in <var class="var">A</var>.
</p>
<p>If <var class="var">k</var> is omitted <code class="code">k = log2 (flintmax) + 1</code> is assumed.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bitcmp (7,4)
  &rArr; 8
dec2bin (11)
  &rArr; 1011
dec2bin (bitcmp (11, 6))
  &rArr; 110100
</pre></div></div>


<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitand">bitand</a>, <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="#XREFbitshift">bitshift</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<p>Octave also includes the ability to left-shift and right-shift values bitwise.
</p>
<a class="anchor" id="XREFbitshift"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bitshift"><span><code class="def-type"><var class="var">B</var> =</code> <strong class="def-name">bitshift</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">k</var>)</code><a class="copiable-link" href="#index-bitshift"> &para;</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-bitshift-1"><span><code class="def-type"><var class="var">B</var> =</code> <strong class="def-name">bitshift</strong> <code class="def-code-arguments">(<var class="var">A</var>, <var class="var">k</var>, <var class="var">n</var>)</code><a class="copiable-link" href="#index-bitshift-1"> &para;</a></span></dt>
<dd><p>Return a <var class="var">k</var> bit shift of <var class="var">n</var>-digit unsigned integers in <var class="var">A</var>.
</p>
<p>A positive <var class="var">k</var> leads to a left shift; A negative value to a right shift.
</p>
<p>If <var class="var">n</var> is omitted it defaults to 64.  <var class="var">n</var> must be in the range [1,64].
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bitshift (eye (3), 1)
&rArr;
</pre><div class="group"><pre class="example-preformatted">2 0 0
0 2 0
0 0 2
</pre></div><pre class="example-preformatted">

bitshift (10, [-2, -1, 0, 1, 2])
&rArr; 2   5  10  20  40
</pre></div></div>

<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbitand">bitand</a>, <a class="ref" href="#XREFbitor">bitor</a>, <a class="ref" href="#XREFbitxor">bitxor</a>, <a class="ref" href="#XREFbitset">bitset</a>, <a class="ref" href="#XREFbitget">bitget</a>, <a class="ref" href="#XREFbitcmp">bitcmp</a>, <a class="ref" href="Integer-Data-Types.html#XREFintmax">intmax</a>, <a class="ref" href="Integer-Data-Types.html#XREFflintmax">flintmax</a>.
</p></dd></dl>


<p>Bits that are shifted out of either end of the value are lost.  Octave
also uses arithmetic shifts, where the sign bit of the value is kept
during a right shift.  For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bitshift (-10, -1)
&rArr; -5
bitshift (int8 (-1), -1)
&rArr; -1
</pre></div></div>

<p>Note that <code class="code">bitshift (int8 (-1), -1)</code> is <code class="code">-1</code> since the bit
representation of <code class="code">-1</code> in the <code class="code">int8</code> data type is <code class="code">[1, 1,
1, 1, 1, 1, 1, 1]</code>.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Logical-Values.html">Logical Values</a>, Previous: <a href="Integer-Data-Types.html">Integer Data Types</a>, Up: <a href="Numeric-Data-Types.html">Numeric Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>