File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -29,13 +29,27 @@ function clearBit(n, i) {
2929}
3030
3131/**
32- * Set bit to either 1 or 0
32+ * Set/clear bit at a given index
3333 * @param n number
3434 * @param i index
3535 * @param b bit
3636 * @returns {number } new number with a given bit set/cleared
3737 */
3838function updateBit ( n , i , b ) {
39+ return ( b > 0 ) ? setBit ( n , i ) : clearBit ( n , i ) ;
40+ }
41+
42+ /**
43+ * Set bit to either 1 or 0
44+ *
45+ * It first clear the bit with a mask and then set it to whatever b is.
46+ *
47+ * @param n number
48+ * @param i index
49+ * @param b bit either 0 or 1. If bigger than 1 value will overflow
50+ * @returns {number } new number with a given bit set/cleared
51+ */
52+ function updateBit2 ( n , i , b ) {
3953 const mask = ~ ( 1 << i ) ;
4054 return ( n & mask ) | ( b << i ) ;
4155}
You can’t perform that action at this time.
0 commit comments