GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript/Bit-Manipulation/SetBit.js at master · hackingstaar/JavaScript Expand file tree/ Copy pathSetBit.jsMore file actions 31 lines (29 loc) · 874 Bytes / Copy pathSetBit.jsFile metadata and controls31 lines (29 loc) · 874 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 /* * Setting Bit: https://www.geeksforgeeks.org/set-k-th-bit-given-number/ * * To set any bit we use bitwise OR (|) operator. * * Bitwise OR (|) compares the bits of the 32 * bit binary representations of the number and * returns a number after comparing each bit. * * 0 | 0 -> 0 * 0 | 1 -> 1 * 1 | 0 -> 1 * 1 | 1 -> 1 * * In-order to set kth bit of a number (where k is the position where bit is to be changed) * we need to shift 1 k times to its left and then perform bitwise OR operation with the * number and result of left shift performed just before. * * References: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR */ /** * @param {number} number * @param {number} bitPosition - zero based. * @return {number} */ export const setBit = (number, bitPosition) => { return number | (1 << bitPosition) } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |