Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `OR`bit operation is used when initializing a variable in embedded programming?

This information is coming from an embedding programming tutorial from YOUTUBE.

The instructor recommends to assign a value to a certain memory location by using an OR operation.

SYSCTL_RCGCGPIO_R    |=   (1U<<5); 

My question is why not just,

SYSCTL_RCGCGPIO_R    =   (1U<<5); 

The definition of SYSCTL_RCGCGPIO_R is

#define SYSCTL_RCGCGPIO_R       (*((volatile unsigned long *)0x400FE608))

Given that the value at the memory location of SYSCTL_RCGCGPIO_R is 0, I understand that both assignments are equal.

But wouldn't first assignment cause unnecessary bit operation?

Is there a special reason for utilizing OR bitwise operation when writing a value to specific memory location?

like image 281
Nguai al Avatar asked Aug 13 '26 23:08

Nguai al


1 Answers

The reason why the tutorial suggests that you use an OR instruction instead of a direct assignment is because the target value may be different from zero due to circumstances that are beyond your control, and you do not want to modify any bits other than bit 6.

like image 180
Mike Nakis Avatar answered Aug 16 '26 20:08

Mike Nakis