Bitwise Operation

Basic Concepts

A bitwise operation operates on a binary number at the level of its individual bits. For example, a variable can be set as a program PSW (PSW), and each bit (flag bit) in the PSW can have a self-defined meaning.

Available APIs

The system provides operations for setting the flag bit to 1 or 0, changing the flag bit content, and obtaining the most significant bit and least significant bit of the flag bit 1 in a PSW. You can also perform bitwise operations on system registers. The following table describes the APIs available for the bitwise operation module. For more details about the APIs, see the API reference.

Table 1 Bitwise operation module APIs

Category

API

Description

Setting the flag bit to 1 or 0

LOS_BitmapSet

Sets a flag bit of a PSW to 1.

LOS_BitmapClr

Sets a flag bit of a PSW to 0.

Obtaining the bit whose flag bit is 1

LOS_HighBitGet

Obtains the most significant bit of 1 in the PSW.

LOS_LowBitGet

Obtains the least significant bit of 1 in the PSW.

Operating continuous bits

LOS_BitmapSetNBits

Sets the continuous flag bits of a PSW to 1.

LOS_BitmapClrNBits

Sets the continuous flag bits of a PSW to 0.

LOS_BitmapFfz

Obtains the first 0 bit starting from the least significant bit (LSB).

Development Example

Example Description

This example implements the following:

  1. Set a flag bit to 1.
  2. Obtain the most significant bit of flag bit 1.
  3. Set a flag bit to 0.
  4. Obtains the least significant bit of the flag bit 1.
#include "los_bitmap.h"
#include "los_printf.h"

static UINT32 BitSample(VOID)
{
  UINT32 flag = 0x10101010;
  UINT16 pos;

  PRINTK("\nBitmap Sample!\n");
  PRINTK("The flag is 0x%8x\n", flag);

  pos = 8;
  LOS_BitmapSet(&flag, pos);
  PRINTK("LOS_BitmapSet:\t pos : %d, the flag is 0x%0+8x\n", pos, flag);

  pos = LOS_HighBitGet(flag);
  PRINTK("LOS_HighBitGet:\t The highest one bit is %d, the flag is 0x%0+8x\n", pos, flag);

  LOS_BitmapClr(&flag, pos);
  PRINTK("LOS_BitmapClr:\t pos : %d, the flag is 0x%0+8x\n", pos, flag);

  pos = LOS_LowBitGet(flag);
  PRINTK("LOS_LowBitGet:\t The lowest one bit is %d, the flag is 0x%0+8x\n\n", pos, flag);

  return LOS_OK;
}

Verification

The development is successful if the return result is as follows:

Bitmap Sample!
The flag is 0x10101010
LOS_BitmapSet: pos : 8,  the flag is 0x10101110
LOS_HighBitGet:The highest one bit is 28, the flag is 0x10101110
LOS_BitmapClr: pos : 28, the flag is 0x00101110
LOS_LowBitGet: The lowest one bit is 4, the flag is 0x00101110