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.

Results 1 to 5 of 5

Thread: Byte Operation

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Byte Operation

    Hi,

    Given a byte 10101010, how to count no. of occurences of 1's ?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Byte Operation

    use bitwise and masking and a loop.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Byte Operation

    Thanks for the suggestion.. do u have the exact code which does this?? And how do i iterarte through the loop for byte ?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Byte Operation

    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.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Byte Operation

    Thank you... Got the solution

Similar Threads

  1. Questions about selection and boolean operation in java3D?
    By freshfish2 in forum Member Introductions
    Replies: 0
    Last Post: February 19th, 2011, 01:24 AM
  2. UTF - 8 / Byte Stream
    By JavaCODER in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 16th, 2010, 02:26 PM
  3. Convert String Literal into Operation
    By SchoolHDD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 30th, 2010, 03:53 PM
  4. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  5. Operation ==
    By meytalg in forum Java Theory & Questions
    Replies: 6
    Last Post: January 4th, 2010, 07:43 PM