Extracting Bits or Bit Fields from an Integer

To extract individual bits see Extracting Bits from a Signal or Elements from an Array.

You can perform the following actions:

To extract a single bit from an integer

  1. Shift the integer such that the bit of interest is in the 0 position.
  2. Apply a bit-wise AND with 1 to isolate the individual bit.

    singleBit = (inputsignal >> BIT)& 1

To extract a bit field from a signal

  1. Shift the input signal such that the least significant bit (LEAST_SIGNIFICANT_BIT) of the bit field that you want to extract is in the 0 position.
  2. Isolate the NUMBER_OF_BITS of the bit field by applying a bit-wise AND with a bitmask.

    bitfield = (inputsignal >> LEAST_SIGNIFICANT_BIT) & ~(~0 << NUMBER_OF_BITS)

See also  

Calculating RMS

Using Enumeration Signals

Applying Calculations to Specific Samples