---
title: Bit Functions
slug: gpc/gpc-scripting-bit-functions
description: Here are the Bit Functions.
icon: ◽
docTags: 
createdAt: 2021-12-08T18:02:05.000Z
---

GPC allows you to manipulate the bits of a given variable. Bit operations are quite complicated, however, there is not really much call for them in the GPC environment and most users will never need them. Therefore, this section will assume you have an understanding of bits, bit masks, and how they correlate with bytes and the binary system.

| Function   | Description                      |
| ---------- | -------------------------------- |
| set\_bit   | Sets one bit.                    |
| clear\_bit | Clears one bit.                  |
| test\_bit  | Tests a bit.                     |
| set\_bits  | Stores a value into a bit index. |
| get\_bits  | Gets a value from the bit index. |

# set\_bit

`set_bit` sets one bit of a variable based on its bit index

:::CodeblockTabs
GPC

```apex
set_bit(a,5);
```
:::

🔴 **Syntax**

:::BlockQuote
set\_bit( \<variable>, \<bit\_index> );
:::

&#x20;⚪ **Parameters**

\<variable>   : the variable to modify
\<bit\_index> : index of the bit to be set with a range of 0 to 15 (16-bit firmware) or 0 to 31 (32-bit firmware).

&#x20;🔵 **Returns**

None

# clear\_bit

`clear_bit` clears one bit of a variable based on its bit index

:::CodeblockTabs
GPC

```apex
clear_bit(a,5);
```
:::

🔴&#x20;**Syntax**

:::BlockQuote
clear\_bit( \<variable> , \<bit\_index> );
:::

&#x20;⚪ **Parameters**

\<variable>   : the variable to modify
\<bit\_index> : index of the bit to be set with a range of 0 to 15 (16-bit firmware) or 0 to 31 (32-bit firmware).

&#x20;🔵 **Returns**

Nothing

# test\_bit

`test_bit` tests a bit index point in a value to check if it is set or not (**TRUE** or **FALSE.**

:::CodeblockTabs
GPC

```apex
test_bit(b,2);
```
:::

&#x20;🔴 **Syntax**

:::BlockQuote
test\_bit( \<value> , \<bit\_index> );
:::

&#x20;⚪ **Parameters**

\<value>       : the value to check
\<bit\_index> : index of the bit to be set with a range of 0 to 15 (16-bit firmware) or 0 to 31 (32-bit firmware).

🔵 **Returns**

**TRUE&#x20;**&#x69;f the bit is set, **FALSE&#x20;**&#x69;f it is not.

# set\_bits

`set_bits` stores a value into a variable based on its bit index and bit mask.

:::CodeblockTabs
GPC

```apex
set_bits(c, 3, 4, 15);
```
:::

&#x20;🔴 **Syntax**

:::BlockQuote
set\_bits( \<variable> , \<value>, \<bit\_index>, \<bit\_mask> );
:::

&#x20;⚪ **Parameters**

\<variable>   : the variable to modify
\<value>       : the value to insert into the variable
\<bit\_index> : the starting index of the bits to be set with a range of 0 to 15 (16-bit firmware) or 0 to 31 (32-bit firmware).
\<bit\_mask> : bit mask to use when inserting the value

🔵 **Returns**

Nothing

# get\_bits

`get_bits` extracts a value from another value based on a bit index and bit mask

:::CodeblockTabs
GPC

```apex
get_bits(c, 4, 15);
```
:::

&#x20;🔴 **Syntax**

:::BlockQuote
get\_bits( \<value> , \<bit\_index>, \<bit\_mask> );
:::

​⚪&#x20;**Parameters&#xD;**&#xA;\<value>       : the value to extract the value from
\<bit\_index> : the starting index of the bits to read with a range of 0 to 15 (16-bit firmware) or 0 to 31 (32-bit firmware).
\<bit\_mask>  : bit mask to use with the final value
​
🔵&#x20;**Returns&#xD;**&#xA;The value shifted into the low bits from the variable used

