To properly display this page you need a browser with JavaScript support.
 User's Manual GPC Language Reference Bit Operations

# Bit Operations

This section describes functions and operators for examining and manipulating bits of a given variable (or value).

Related GPC Functions:

set_bit    Set one bit of a variable, based on bit position
clear_bit  Clear one bit of a variable, based on bit position
test_bit   Test a bit into of a variable (or value), based on bit position
set_bits   Store a value into a variable, based on bit position and a bit mask
get_bits   Extract a value from a variable (or value), based on bit position and a bit mask

1. set_bit

Set one bit of a variable, based on bit position.

Prototype:

set_bit ( <variable>, <bit_index> )

Parameters:

<variable>   : any defined variable
<bit_index>  : position of the bit to be set in <variable>, range [0~15]

Return:

None

Example:

 set_bit(x, 3);

2. clear_bit

Clear one bit of a variable, based on bit position.

Prototype:

clear_bit ( <variable>, <bit_index> )

Parameters:

<variable>   : any defined variable
<bit_index>  : position of the bit to be clear in <variable>, range [0~15]

Return:

None

Example:

 clear_bit(x, 9);

3. test_bit

Test a bit into of a variable (or value), based on bit position.

Prototype:

int test_bit ( <value>, <bit_index> )

Parameters:

<value>      : anything that has a value (constants, variables, functions, expressions, etc...)
<bit_index>  : position of the bit to be tested in <value>, range [0~15]

Return:

True if the bit is set, false otherwise

Example:

 if(test_bit(x, 12)) {     // Do something }

4. set_bits

Store a value into a variable, based on bit position and a bit mask.

Prototype:

set_bits ( <variable>, <value>, <bit_index>, <bit_mask> )

Parameters:

<variable>   : any defined variable
<value>      : anything that has a value (constants, variables, functions, expressions, etc...)
<bit_index>  : position of the less significant bit in the value to store, range [0~15]
<bit_mask>   : bit mask corresponding to the size, in bits, of the value to store (without shifting)

Return:

None

Example:

 int m = 0, n = 0; main {     set_bits(m, 5, 4, 15); // bit_mask = 15, 0x000F,     // m = 80 (0x0050)        0b0000'0000'0000'1111       set_bits(m, 109, 8, 255); // bit_mask = 255, 0x00FF,     // m = 27984 (0x6D50)        0b0000'0000'1111'1111       n = get_bits(m, 4, 15); // n = 5 }

5. get_bits

Extract a value from a variable (or a given value defined by a expression), based on bit position and a bit mask.

Prototype:

int get_bits ( <expression>, <bit_index>, <bit_mask> )

Parameters:

<expression> : a variable or anything that has a value
<bit_index>  : position of the less significant bit in the value to extract, range [0~15]
<bit_mask>   : bit mask corresponding to the size, in bits, of the value to extract (without shifting)

Return:

returns the value of a subset of bits in <expression>, the subset is defined by <bit_index> and <bit_mask>

Example:

 int m = 0, n = 0; main {     set_bits(m, 5, 4, 15); // bit_mask = 15, 0x000F,     // m = 80 (0x0050)        0b0000'0000'0000'1111       set_bits(m, 109, 8, 255); // bit_mask = 255, 0x00FF,     // m = 27984 (0x6D50)        0b0000'0000'1111'1111       n = get_bits(m, 8, 255); // n = 109 }