Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kernighan's Algorithm for Counting Set Bits #1107

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions Bit-Manipulation/CountSetBits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'
// author: Smit Gabani
// https://github.com/smitgabani

/**
* explanation link: https://iq.opengenus.org/brian-kernighan-algorithm/
* Brain Kernighan's Algorithm for Counting Set Bits
* @param: num takes number whose number of set bit is to be found
* @return: the count of set bits in the binary equivalent
* */
function CountSetBits (num) {
let cnt = 0
while (num > 0) {
cnt++
num &= (num - 1)
}
return cnt
}
// console.log(CountSetBits(10))
// expected output: 2

export { CountSetBits }
22 changes: 22 additions & 0 deletions Bit-Manipulation/test/CountSetBits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CountSetBits } from '../CountSetBits'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more thinking something like:

test("CountSetBits", () => {
    const tests = {
        // binary representation: set bits
        '1001': 2,
        '111': 3,
    }
    for (binary in tests)
         expect(CountSetBits(parseInt(binary, 2))).toBe(tests[binary])
})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not use quoted for keys because of:
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not intend a reversal to the more verbose form. You can even use a list of lists if you like - all you need is a convenient way to test that a certain input leads to a certain output without copy-pasting. Here's another alternative:

const testCountSetBits = (input, expected_output) => expect(CountSetBits(input)).toBe(expected_output)
test('CountSetBits', () => {
    testCountSetBits(25, 3)
    testCountSetBits(36, 2)
})

etc., or, IMO preferable since it makes the binary representation apparent:

const testCountSetBits = (input_bin_str, expected_output) => expect(CountSetBits(parseInt(input, 2))).toBe(expected_output)
test('CountSetBits', () => {
    testCountSetBits("0", 0)
    testCountSetBits("10", 1)
})

etc.


test('check CountSetBits of 25 is 3', () => {
const res = CountSetBits(25)
expect(res).toBe(3)
})
test('check CountSetBits of 36 is 2', () => {
const res = CountSetBits(36)
expect(res).toBe(2)
})
test('check CountSetBits of 16 is 1', () => {
const res = CountSetBits(16)
expect(res).toBe(1)
})
test('check CountSetBits of 58 is 4', () => {
const res = CountSetBits(58)
expect(res).toBe(4)
})
test('check CountSetBits of 0 is 0', () => {
const res = CountSetBits(0)
expect(res).toBe(0)
})