Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  66] [ 4]  / answers: 1 / hits: 68969  / 15 Years ago, thu, september 17, 2009, 12:00:00

How to set, clear, toggle and check a bit in JavaScript?


More From » numbers

 Answers
10

To get a bit mask:



var mask = 1 << 5; // gets the 6th bit


To test if a bit is set:



if ((n & mask) != 0) {
// bit is set
} else {
// bit is not set
}


To set a bit:



n |= mask;


To clear a bit:



n &= ~mask;


To toggle a bit:



n ^= mask;


Refer to the Javascript bitwise operators.


[#98679] Sunday, September 13, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jalyn

Total Points: 173
Total Questions: 96
Total Answers: 90

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;