From 51a9126053b66c0db8b639142d8e9f86ae2b8fd2 Mon Sep 17 00:00:00 2001 From: AbhinavXT Date: Sun, 4 Jul 2021 21:47:53 +0530 Subject: [PATCH 1/3] Added SetBit.js, SetBit.test.js in Bit-Manipulation directory --- Bit-Manipulation/SetBit.js | 31 ++++++++++++++++++++++++++++ Bit-Manipulation/test/SetBit.test.js | 21 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Bit-Manipulation/SetBit.js create mode 100644 Bit-Manipulation/test/SetBit.test.js diff --git a/Bit-Manipulation/SetBit.js b/Bit-Manipulation/SetBit.js new file mode 100644 index 000000000..f7774dd54 --- /dev/null +++ b/Bit-Manipulation/SetBit.js @@ -0,0 +1,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) +} diff --git a/Bit-Manipulation/test/SetBit.test.js b/Bit-Manipulation/test/SetBit.test.js new file mode 100644 index 000000000..7a4d0dcd7 --- /dev/null +++ b/Bit-Manipulation/test/SetBit.test.js @@ -0,0 +1,21 @@ +import { setBit } from '../SetBit' + +test('should set bit at the given bit Position', () => { + const setBitPos = setBit(1, 0) + expect(setBitPos).toBe(1) +}) + +test('should set bit at the given bit Position', () => { + const setBitPos = setBit(1, 0) + expect(setBitPos).toBe(1) +}) + +test('should set bit at the given bit Position', () => { + const setBitPos = setBit(10, 1) + expect(setBitPos).toBe(10) +}) + +test('should set bit at the given bit Position', () => { + const setBitPos = setBit(10, 2) + expect(setBitPos).toBe(14) +}) From 58fca82ec3b19f84500fb0c5d05443ef043f21d2 Mon Sep 17 00:00:00 2001 From: AbhinavXT Date: Mon, 5 Jul 2021 11:09:46 +0530 Subject: [PATCH 2/3] Changed Test statement --- Bit-Manipulation/test/SetBit.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Bit-Manipulation/test/SetBit.test.js b/Bit-Manipulation/test/SetBit.test.js index 7a4d0dcd7..f16e32929 100644 --- a/Bit-Manipulation/test/SetBit.test.js +++ b/Bit-Manipulation/test/SetBit.test.js @@ -1,21 +1,21 @@ import { setBit } from '../SetBit' -test('should set bit at the given bit Position', () => { +test('Set bit number 0 in 1:', () => { const setBitPos = setBit(1, 0) expect(setBitPos).toBe(1) }) -test('should set bit at the given bit Position', () => { +test('Set bit number 0 in 1:', () => { const setBitPos = setBit(1, 0) expect(setBitPos).toBe(1) }) -test('should set bit at the given bit Position', () => { +test('Set bit number 0 in 1:', () => { const setBitPos = setBit(10, 1) expect(setBitPos).toBe(10) }) -test('should set bit at the given bit Position', () => { +test('Set bit number 0 in 1:', () => { const setBitPos = setBit(10, 2) expect(setBitPos).toBe(14) }) From 504dbb0c30475ccb5996f49ec4610fde4a6cae98 Mon Sep 17 00:00:00 2001 From: AbhinavXT Date: Mon, 5 Jul 2021 16:25:23 +0530 Subject: [PATCH 3/3] Statement changes Added --- Bit-Manipulation/test/SetBit.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Bit-Manipulation/test/SetBit.test.js b/Bit-Manipulation/test/SetBit.test.js index f16e32929..0fc0a1d4b 100644 --- a/Bit-Manipulation/test/SetBit.test.js +++ b/Bit-Manipulation/test/SetBit.test.js @@ -5,17 +5,17 @@ test('Set bit number 0 in 1:', () => { expect(setBitPos).toBe(1) }) -test('Set bit number 0 in 1:', () => { - const setBitPos = setBit(1, 0) - expect(setBitPos).toBe(1) +test('Set bit number 0 in 2:', () => { + const setBitPos = setBit(2, 0) + expect(setBitPos).toBe(3) }) -test('Set bit number 0 in 1:', () => { +test('Set bit number 1 in 10:', () => { const setBitPos = setBit(10, 1) expect(setBitPos).toBe(10) }) -test('Set bit number 0 in 1:', () => { +test('Set bit number 2 in 10:', () => { const setBitPos = setBit(10, 2) expect(setBitPos).toBe(14) })