How do you set, clear, and toggle a single bit?

Digital Marketing Facebook Marketing API 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ How do you set, clear, and toggle a bit?

Posted on 16 Aug 2022, this text provides information on Facebook Marketing API related to Digital Marketing. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Setting a bit Use the bitwise OR operator (|) to set a bit. number |= 1UL << n; That will set the nth bit of number. n should be zero, if you want to set the 1st bit and so on upto n-1, if you want to set the nth bit. Use 1ULL if number is wider than unsigned long; promotion of 1UL << n doesn't happen until after evaluating 1UL << n where it's undefined behaviour to shift by more than the width of a long. The same applies to all the rest of the examples. Clearing a bit Use the bitwise AND operator (&) to clear a bit. number &= ~(1UL << n); That will clear the nth bit of number. You must invert the bit string with the bitwise NOT operator (~), then AND it. Toggling a bit The XOR operator (^) can be used to toggle a bit. number ^= 1UL << n; That will toggle the nth bit of number. Checking a bit You didn't ask for this, but I might as well add it. To check a bit, shift the number n to the right, then bitwise AND it: bit = (number >> n) & 1U; That will put the value of the nth bit of number into the variable bit. Changing the nth bit to x Setting the nth bit to either 1 or 0 can be achieved with the following on a 2's complement C++ implementation: number ^= (-x ^ number) & (1UL << n); Bit n will be set if x is 1, and cleared if x is 0. If x has some other value, you get garbage. x = !!x will booleanize it to 0 or 1. To make this independent of 2's complement negation behaviour (where -1 has all bits set, unlike on a 1's complement or sign/magnitude C++ implementation), use unsigned negation. number ^= (-(unsigned long)x ^ number) & (1UL << n); or unsigned long newbit = !!x; // Also booleanize to force 0 or 1 number ^= (-newbit ^ number) & (1UL << n); It's generally a good idea to use unsigned types for portable bit manipulation. or number = (number & ~(1UL << n)) | (x << n); (number & ~(1UL << n)) will clear the nth bit and (x << n) will set the nth bit to x. It's also generally a good idea to not to copy/paste code in general and so many people use preprocessor macros (like the community wiki answer further down) or some sort of encapsulation.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community