Friday, July 24, 2009

continued..

bitcmp

Complement bits

Syntax

  • C = bitcmp(A,n)

Description

C = bitcmp(A,n) returns the bit-wise complement of A as an n-bit floating-point integer (flint).

Example

With eight-bit arithmetic, the ones' complement of 01100011 (99, decimal) is 10011100 (156, decimal).

  • C = bitcmp(99,8)

    C =

    156
bitget

Get bit

Syntax

  • C = bitget(A,bit)

Description

C = bitget(A,bit) returns the value of the bit at position bit in A. Operand A must be a nonnegative integer, and bit must be a number between 1 and the number of bits in the floating-point integer (flint) representation of A (52 for IEEE flints). To ensure the operand is an integer, use the ceil, fix, floor, and round functions.

Example

The dec2bin function converts decimal numbers to binary. However, you can also use the bitget function to show the binary representation of a decimal number. Just test successive bits from most to least significant:

  • disp(dec2bin(13))
    1101
    C = bitget(13,4:-1:1)

    C =
    1 1 0 1
bitmax

Maximum floating-point integer

Syntax

  • bitmax

Description

bitmax returns the maximum unsigned floating-point integer for your computer. It is the value when all bits are set, namely the value

bitor

Bit-wise OR

Syntax

  • C = bitor(A,B)

Description

C = bitor(A,B) returns the bit-wise OR of two nonnegative integer arguments A and B. To ensure the operands are integers, use the ceil, fix, floor, and round functions.

Examples

The five-bit binary representations of the integers 13 and 27 are 01101 and 11011, respectively. Performing a bit-wise OR on these numbers yields 11111, or 31.

  • C = bitor(13,27)

    C =

    31
bitset

Set bit

Syntax

  • C = bitset(A,bit)
    C = bitset(A,bit,v)

Description

C = bitset(A,bit) sets bit position bit in A to 1 (on). A must be a nonnegative integer and bit must be a number between 1 and the number of bits in the floating-point integer (flint) representation of A (52 for IEEE flints). To ensure the operand is an integer, use the ceil, fix, floor, and round functions.

C = bitset(A,bit,v) sets the bit at position bit to the value v, which must be either 0 or 1.

Examples

Setting the fifth bit in the five-bit binary representation of the integer 9 (01001) yields 11001, or 25.

  • C = bitset(9,5)

    C =

    25
bitshift

Bit-wise shift

Syntax

  • C = bitshift(A,k,n)

C = bitshift(A,k)

Description

C = bitshift(A,k,n) returns the value of A shifted by k bits. If k>0, this is same as a multiplication by 2k (left shift). If k<0, this is the same as a division by 2k (right shift). An equivalent computation for this function is
C = fix(A*2^k).

If the shift causes C to overflow n bits, the overflowing bits are dropped. A must contain nonnegative integers between 0 and BITMAX, which you can ensure by using the ceil, fix, floor, and round functions.

C = bitshift(A,k) uses the default value of n = 53.

Examples

Shifting 1100 (12, decimal) to the left two bits yields 110000 (48, decimal).

  • C = bitshift(12,2)

    C =

    48
bitxor

Bit-wise XOR

Syntax

  • C = bitxor(A,B)

Description

C = bitxor(A,B) returns the bit-wise XOR of the two arguments A and B. Both A and B must be integers. You can ensure this by using the ceil, fix, floor, and round functions.

Examples

The five-bit binary representations of the integers 13 and 27 are 01101 and 11011, respectively. Performing a bit-wise XOR on these numbers yields 10110, or 22.

  • C = bitxor(13,27)

    C =
    22

No comments:

Post a Comment