Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
7
of 9 vote

That's pretty simple. Each time you do X &= X-1 it erases the lowest non-zero bit. You repeat it and it removes one bit at a time, until all bits are gone. So that gives you the answer how many bits existed in the original integer.

For example, if the lowest bit is 1, after subtracting 1 it becomes 0. So AND the 1 bit and the 0 bit gives you 0, the bit is removed. All other bits are intact.

Next if you have the lowest bits as 100, subtracting 1 gives 011. AND them together, the lowest three bits cancel out. So that removes another lowest bit, with other bits intact.

Pretty useful trick to quickly count all bits without using if/else.

- Anthony Mai October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
6
of 6 vote

1) It will return number of 1s in the number.
eg: If input is 7 (0b0111) output will be 3
2) If the return value is 1, then it is a power of 2
eg: If input is 8 (0b1000) output will be 1

- maverick202 October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 vote

Counts the number of bits which are ON in a given inputted number .

- shukad333 October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

x = 5

101 5
100 5-1
------
100 4
011 4-1
------
000 0

count of bit 1 == 2


---------------------------------
x = 10

1010 10
1001 10-1
------
1000 9
0111 9-1
------
0000 0

count of bit 1 == 2


---------------------------------

111010
111001
--------
111000
110111
--------
110000
101111
--------
100000
011111
--------
000000 0

count of bit 1 == 4

- holylance82 October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it is more like %. Do "i++" until the highest bit of the num is encountered. We get the rest of bits of the num.

- Anonymous October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It counts the number of ones in the integer

- HH October 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this loop will be continue till max integer number

- Anonymous October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Return total number of set bits in x.

- khunima October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a fast algorithm to check whether a positive integer is a power of 2 or not. This works only if x>0.

- Harsha October 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very nice trick to count 1 bites.

- AK October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

number of bits set in given number

- nex February 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No of bits set in the input

- MK March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It returns number of set bits in a number. While counting you are also erasing that set bit.

- rayasamharish September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.It will return number of 1s in the number
2. i-1 had on advantage - least number to be subtracted from x to make it a power of 2

- nk December 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

total count of set bits in the given number.

- hilleybilley January 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It gives number of SET bits of given number..

- Shreekant March 21, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More