Next: , Previous: Arithmetic, Up: Top


19 Bit Manipulations

Octave provides the following functions for bit twiddling.

— Function File: x = bitset (a, n)
— Function File: x = bitset (a, n, v)

Set or reset bit(s) n of unsigned integers in a. v = 0 resets and v = 1 sets the bits. The lowest significant bit is: n = 1

          dec2bin (bitset (10, 1))
          => 1011
     
     
     
See also: bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax.

— Function File: X = bitget (a,n)

Return the status of bit(s) n of unsigned integers in a the lowest significant bit is n = 1.

          bitget (100, 8:-1:1)
          => 0  1  1  0  0  1  0  0
     
     
     
See also: bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax.

— Function File: bitcmp (a, k)

Return the k-bit complement of integers in a. If k is omitted k = log2 (bitmax) + 1 is assumed.

          bitcmp(7,4)
          => 8
          dec2bin(11)
          => 1011
          dec2bin(bitcmp(11, 6))
          => 110100
     
     
     
See also: bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax.

— Function File: bitshift (a, k)
— Function File: bitshift (a, k, n)

Return a k bit shift of n- digit unsigned integers in a. A positive k leads to a left shift. A negative value to a right shift. If n is omitted it defaults to log2(bitmax)+1. n must be in range [1,log2(bitmax)+1] usually [1,33]

          bitshift (eye (3), 1))
          =>
          2 0 0
          0 2 0
          0 0 2
          
          bitshift (10, [-2, -1, 0, 1, 2])
          => 2   5  10  20  40
          
          
          
     
     
     
See also: bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax.

— Built-in Function: bitand (x, y)

Return the bitwise AND of nonnegative integers. x, y must be in range [0..bitmax]

     
     
See also: bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax.

— Built-in Function: bitor (x, y)

Return the bitwise OR of nonnegative integers. x, y must be in range [0..bitmax]

     
     
See also: bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax.

— Built-in Function: bitxor (x, y)

Return the bitwise XOR of nonnegative integers. x, y must be in range [0..bitmax]

     
     
See also: bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax.

— Built-in Function: bitmax ()

Return the largest integer that can be represented as a floating point value. On IEEE-754 compatiable systems, bitmax is 2^53 - 1.