<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.13"> <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"> <a name="Bit-Manipulations"></a> <p> 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>