> For the complete documentation index, see [llms.txt](https://ykkim.gitbook.io/ec/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ykkim.gitbook.io/ec/ec-course/tutorial/tutorial-bitwise-macro.md).

# Tutorial: Bitwise Macro

## Bitwise Operation Macro

Instead of using bitwise operations in C programming, which can be confusing to some students, we can use simple Macros for bitwise operations

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FCoHZ1oRtMTHDIjK6BAQs%2Fimage.png?alt=media&amp;token=54f5a34f-6fb2-4dfb-9e68-28e4b5d6951c" alt=""><figcaption></figcaption></figure>

### Example:

```cpp

// GPIO Output Set
GPIOA->ODR |= (1UL << LED_PIN);	 		
BIT_SET(GPIOA->ODR, LED_PIN);

// GPIO Output Clear
GPIOA->ODR &= ~(1UL << LED_PIN);	 	
BIT_CLEAR(GPIOA->ODR, LED_PIN);


// GPIO Mode Register 
GPIOA->MODER &= ~(3UL<<(2*LED_PIN)); 	// Clear as `b11=0x3` starting from  2*LED_PIN
BITS_CLEAR(GPIOA->MODER, 2 * LED_PIN, 3); // Clear as `b11=0x3` starting from  2*LED_PIN

GPIOA->MODER |=   3UL<<(2*LED_PIN);   // Set '11' for Pin 5
BITS_SET(GPIOA->MODER, 2 * LED_PIN, 3);


```

### Exercise

Modify `ecPinNames.h` to include the Bitwise Macro.

```c++
#ifndef EC_PINNAMES_H
#define EC_PINNAMES_H

#include "stm32f411xe.h"

#ifdef __cplusplus
extern "C" {
#endif


// Bitwise Macro Definition
#define BIT_SET(REG, BIT)      	((REG) |= 1<< (BIT))
#define BIT_CLEAR(REG, BIT)     ((REG) &= ~1<<(BIT))
#define BIT_READ(REG, BIT)      ((REG)>>BIT & (1))
#define BITS_SET(REG, BIT,NUM)     ((REG) |= NUM<< (BIT))
#define BITS_CLEAR(REG, BIT,NUM)   ((REG) &= ~(NUM<< (BIT))


// ...


```
