To change a specific bit to 0/1 in C/C++

Asked 2 years ago, Updated 2 years ago, 30 views

I want to know how to change a specific bit to 0/1 in C/C++.

c++ c bit-mamipulation

2022-09-21 20:06

1 Answers

Use the bitwise OR operator (|). number |= << x; sets the x-th bit to 1.

Use the bitwise AND operator (&). number &= ~(1<<x); sets the x-th bit to zero

Use the XOR operator (). number ^= 1 << x; replaces the x-th bit with 0 if it is 1, and changes to 1 if it is 0.

)

Use the bitwise AND operator (&). (number >> x ) & 1; returns 1 if the x-th bit is 1, and 0 if 0 is 0.


2022-09-21 20:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.