DEV Community

Kishore kunal
Kishore kunal

Posted on

#day 17 of solving [48/450] questions

Next few post will all be about solving bitwise question.

Single-number

return (x := 0, [x := x ^ v for v in nums])[-1][-1]
Enter fullscreen mode Exit fullscreen mode

Image description

number-of-1-bit

Approach 1 : using in-built fn to count 1 bits

Image description

Approach 2:

Think of a number in binary n = XXXXXX1000, n - 1 is XXXXXX0111. n & (n - 1) will be XXXXXX0000 which is just remove the last significant 1****

Image description

counting-bits/
Image description
self made one liner

missing-number

Two Approach:
1) total_sum - n*(n+1)/2
2) xor of (1........n) ^(1...............n) == missing number

Image description
Image description

Top comments (0)