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
|
<html lang="en">
<head>
<title>Bit Manipulations - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Numeric-Data-Types.html#Numeric-Data-Types" title="Numeric Data Types">
<link rel="prev" href="Integer-Data-Types.html#Integer-Data-Types" title="Integer Data Types">
<link rel="next" href="Logical-Values.html#Logical-Values" title="Logical Values">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Bit-Manipulations"></a>
Next: <a rel="next" accesskey="n" href="Logical-Values.html#Logical-Values">Logical Values</a>,
Previous: <a rel="previous" accesskey="p" href="Integer-Data-Types.html#Integer-Data-Types">Integer Data Types</a>,
Up: <a rel="up" accesskey="u" href="Numeric-Data-Types.html#Numeric-Data-Types">Numeric Data Types</a>
<hr>
</div>
<h3 class="section">4.5 Bit Manipulations</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>bitset</code> and <code>bitget</code>.
<!-- ./general/bitset.m -->
<p><a name="doc_002dbitset"></a>
<div class="defun">
— Function File: <var>x</var> = <b>bitset</b> (<var>a, n</var>)<var><a name="index-bitset-250"></a></var><br>
— Function File: <var>x</var> = <b>bitset</b> (<var>a, n, v</var>)<var><a name="index-bitset-251"></a></var><br>
<blockquote><p>Set or reset bit(s) <var>n</var> of unsigned integers in <var>a</var>.
<var>v</var> = 0 resets and <var>v</var> = 1 sets the bits.
The lowest significant bit is: <var>n</var> = 1
<pre class="example"> dec2bin (bitset (10, 1))
1011
</pre>
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitand.html#doc_002dbitand">bitand</a>, <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<!-- ./general/bitget.m -->
<p><a name="doc_002dbitget"></a>
<div class="defun">
— Function File: <var>X</var> = <b>bitget</b> (<var>a,n</var>)<var><a name="index-bitget-252"></a></var><br>
<blockquote><p>Return the status of bit(s) <var>n</var> of unsigned integers in <var>a</var>
the lowest significant bit is <var>n</var> = 1.
<pre class="example"> bitget (100, 8:-1:1)
0 1 1 0 0 1 0 0
</pre>
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitand.html#doc_002dbitand">bitand</a>, <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<p>The arguments to all of Octave's bitwise operations can be scalar or
arrays, except for <code>bitcmp</code>, whose <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
<pre class="example"> bitget (100, 8:-1:1)
</pre>
<p>is the same as
<pre class="example"> bitget (100 * ones (1, 8), 8:-1:1)
</pre>
<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>bitset</code> above passes the floating point value
<code>10</code>, it is treated as the bits <code>[1, 0, 1, 0]</code> rather than the
bits of the native floating point format representation of <code>10</code>.
<p>As the maximum value that can be represented by a number is important
for bit manipulation, particularly when forming masks, Octave supplies
the function <code>bitmax</code>.
<!-- bitfcns.cc -->
<p><a name="doc_002dbitmax"></a>
<div class="defun">
— Built-in Function: <b>bitmax</b> ()<var><a name="index-bitmax-253"></a></var><br>
<blockquote><p>Return the largest integer that can be represented as a floating point
value. On IEEE-754 compatible systems, <code>bitmax</code> is <code>2^53 - 1</code>.
</p></blockquote></div>
<p>This is the double precision version of the functions <code>intmax</code>,
previously discussed.
<p>Octave also includes the basic bitwise 'and', 'or' and 'exclusive or'
operators.
<!-- bitfcns.cc -->
<p><a name="doc_002dbitand"></a>
<div class="defun">
— Built-in Function: <b>bitand</b> (<var>x, y</var>)<var><a name="index-bitand-254"></a></var><br>
<blockquote><p>Return the bitwise AND of non-negative integers.
<var>x</var>, <var>y</var> must be in the range [0,bitmax]
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<!-- bitfcns.cc -->
<p><a name="doc_002dbitor"></a>
<div class="defun">
— Built-in Function: <b>bitor</b> (<var>x, y</var>)<var><a name="index-bitor-255"></a></var><br>
<blockquote><p>Return the bitwise OR of non-negative integers.
<var>x</var>, <var>y</var> must be in the range [0,bitmax]
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<!-- bitfcns.cc -->
<p><a name="doc_002dbitxor"></a>
<div class="defun">
— Built-in Function: <b>bitxor</b> (<var>x, y</var>)<var><a name="index-bitxor-256"></a></var><br>
<blockquote><p>Return the bitwise XOR of non-negative integers.
<var>x</var>, <var>y</var> must be in the range [0,bitmax]
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitand.html#doc_002dbitand">bitand</a>, <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<p>The bitwise 'not' 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's
bitwise 'not' operator is <code>bitcmp</code>.
<!-- ./general/bitcmp.m -->
<p><a name="doc_002dbitcmp"></a>
<div class="defun">
— Function File: <b>bitcmp</b> (<var>a, k</var>)<var><a name="index-bitcmp-257"></a></var><br>
<blockquote><p>Return the <var>k</var>-bit complement of integers in <var>a</var>. If
<var>k</var> is omitted <code>k = log2 (bitmax) + 1</code> is assumed.
<pre class="example"> bitcmp(7,4)
8
dec2bin(11)
1011
dec2bin(bitcmp(11, 6))
110100
</pre>
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitand.html#doc_002dbitand">bitand</a>, <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitshift.html#doc_002dbitshift">bitshift</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<p>Octave also includes the ability to left-shift and right-shift values bitwise.
<!-- bitfcns.cc -->
<p><a name="doc_002dbitshift"></a>
<div class="defun">
— Built-in Function: <b>bitshift</b> (<var>a, k</var>)<var><a name="index-bitshift-258"></a></var><br>
— Built-in Function: <b>bitshift</b> (<var>a, k, n</var>)<var><a name="index-bitshift-259"></a></var><br>
<blockquote><p>Return a <var>k</var> bit shift of <var>n</var>-digit unsigned
integers in <var>a</var>. A positive <var>k</var> leads to a left shift.
A negative value to a right shift. If <var>n</var> is omitted it defaults
to log2(bitmax)+1.
<var>n</var> must be in the range [1,log2(bitmax)+1] usually [1,33]
<pre class="example"> bitshift (eye (3), 1)
<p>2 0 0
0 2 0
0 0 2
bitshift (10, [-2, -1, 0, 1, 2])
2 5 10 20 40
<!-- FIXME - restore this example when third arg is allowed to be an array. -->
<!-- bitshift ([1, 10], 2, [3,4]) -->
<!-- @result{} 4 8 -->
</pre>
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002dbitand.html#doc_002dbitand">bitand</a>, <a href="doc_002dbitor.html#doc_002dbitor">bitor</a>, <a href="doc_002dbitxor.html#doc_002dbitxor">bitxor</a>, <a href="doc_002dbitset.html#doc_002dbitset">bitset</a>, <a href="doc_002dbitget.html#doc_002dbitget">bitget</a>, <a href="doc_002dbitcmp.html#doc_002dbitcmp">bitcmp</a>, <a href="doc_002dbitmax.html#doc_002dbitmax">bitmax</a>.
</p></blockquote></div>
<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
<pre class="example"> bitshift (-10, -1)
-5
bitshift (int8 (-1), -1)
-1
</pre>
<p>Note that <code>bitshift (int8 (-1), -1)</code> is <code>-1</code> since the bit
representation of <code>-1</code> in the <code>int8</code> data type is <code>[1, 1,
1, 1, 1, 1, 1, 1]</code>.
</body></html>
|