Given an integer of a certain bit length, does it have an even or odd number of parity bits?
0 Answers
Given an integer of a certain bit length, does it have an even or odd number of parity bits?
| Flag | PURGE
The code is
public static Integer evenParity(Integer number){
return (number == 0) ? 0 : ((number&1) + evenParity(number>>1))%2;
}
I am a newbe, I have a basic question here. We need to count the number of ones and if they are odd, then its odd or even parity bit. Now, when we look at the code, the code keeps right shifting the number until it reaches '0'. So, we are counting bits from different number than the one passed.My, understanding was we should count the one bits of a given number without chaining the number. Even though this code is working correctly I'm not able to understand how this is working.
Thanks
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.