Hi,
Given a byte 10101010, how to count no. of occurences of 1's ?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Given a byte 10101010, how to count no. of occurences of 1's ?
use bitwise and masking and a loop.
Thanks for the suggestion.. do u have the exact code which does this?? And how do i iterarte through the loop for byte ?
Querying the status of a bit (via bit-masking)
Once you know how to query the status of a bit, it's not too difficult to iterate through all the bits you have. You can use shift operators to determine which bit you want to mask.
mask = 1 << bit_number; // shift 1 over to which ever bit you want to mask
Last edited by helloworld922; March 27th, 2011 at 03:30 AM.
Thank you... Got the solution