1# Bitwise Operation 2 3 4## Basic Concepts 5 6A bitwise operation operates on the bits of a binary number. A variable can be set as a program status word (PSW), and each bit (flag bit) in the PSW can have a self-defined meaning. 7 8 9## **Available APIs** 10 11The system provides operations for setting the flag bit to **1** or **0**, changing the flag bit content, and obtaining the most significant bit (MSB) and least significant bit (LSB) 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. 12 13 **Table 1** APIs of the bitwise operation module 14 15| Category | API Description | 16| -------- | -------- | 17| Setting a flag bit| - **LOS_BitmapSet**: sets a flag bit of a PSW to **1**.<br>- **LOS_BitmapClr**: sets a flag bit of a PSW to **0**. | 18| Obtaining the bit whose flag bit is **1**| -**LOS_HighBitGet**: obtains the most significant bit of 1 in a PSW.<br>- **LOS_LowBitGet**: obtains the least significant bit of 1 in a PSW. | 19| Operating continuous bits| - **LOS_BitmapSetNBits**: sets the consecutive flag bits of a PSW to **1**.<br>- **LOS_BitmapClrNBits**: sets the consecutive flag bits of a PSW to **0**.<br>- **LOS_BitmapFfz**: obtains the first 0 bit starting from the LSB. | 20 21 22## Development Example 23 24 25### Example Description 26 27This example implements the following: 28 291. Set a flag bit to **1**. 30 312. Obtain the MSB of flag bit 1. 32 333. Set a flag bit to **0**. 34 354. Obtain the LSB of flag bit 1. 36 37### Sample Code 38 39The sample code can be compiled and verified in **./kernel/liteos_a/testsuites/kernel/src/osTest.c**. The **BitSample** function is called in **TestTaskEntry**. 40 41``` 42#include "los_bitmap.h" 43#include "los_printf.h" 44 45static UINT32 BitSample(VOID) 46{ 47 UINT32 flag = 0x10101010; 48 UINT16 pos; 49 50 PRINTK("\nBitmap Sample!\n"); 51 PRINTK("The flag is 0x%8x\n", flag); 52 53 pos = 8; 54 LOS_BitmapSet(&flag, pos); 55 PRINTK("LOS_BitmapSet:\t pos : %d, the flag is 0x%0+8x\n", pos, flag); 56 57 pos = LOS_HighBitGet(flag); 58 PRINTK("LOS_HighBitGet:\t The highest one bit is %d, the flag is 0x%0+8x\n", pos, flag); 59 60 LOS_BitmapClr(&flag, pos); 61 PRINTK("LOS_BitmapClr:\t pos : %d, the flag is 0x%0+8x\n", pos, flag); 62 63 pos = LOS_LowBitGet(flag); 64 PRINTK("LOS_LowBitGet:\t The lowest one bit is %d, the flag is 0x%0+8x\n\n", pos, flag); 65 66 return LOS_OK; 67} 68``` 69 70 71### Verification 72 73The development is successful if the return result is as follows: 74 75 76``` 77Bitmap Sample! 78The flag is 0x10101010 79LOS_BitmapSet: pos : 8, the flag is 0x10101110 80LOS_HighBitGet:The highest one bit is 28, the flag is 0x10101110 81LOS_BitmapClr: pos : 28, the flag is 0x00101110 82LOS_LowBitGet: The lowest one bit is 4, the flag is 0x00101110 83``` 84