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: Helph me !

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    Honduras
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Helph me !

    Hello that I am trying to make a function to tell me how to know if a byte is powered on or not but it must be through a mask depending on the position you send

    bool estaEncendido(char byte,int pos)
    {
    //  byte = byte<<(7-pos) ;
      //byte = byte>>(pos+1);
      int mask=1;
      int result =1;
      for(pos=0; pos<8; pos++, mask <<=1)
      {
        if((byte & mask) !=0)
        {
            result = 0;
            break;
        }
        //return byte;
      }
    }

    In the main should have been as well

    System.out.print(estaEncendido(5/*000000101*/,2/*00000100*/));
    /*00000101 */
    /*00000100 &*/
    /*00000100*/This ignition

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Helph me !

    Use the AND operator (&) to test if a bit is on. The results will be zero if the bit is not on.

    Can you explain what problem you are having?
    If you need help with some code: Post a small, complete program that compiles, executes and shows the problem. Execute the program, copy its output and paste it here and explain what is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Helph me !

    I'm not sure I understand what you're asking, but consider this:

    If you AND the existing register with a value that has ONLY the desired bit set to 1, then the result will either be all zeros or the same as the value you passed. If all zeros, the bit in question was not set. If the result is the same as that passed, then the desired value is set to one.

    For example, if the status register is:

    11010100

    and I want to know if the 4th bit from the right is set, then I would AND the above with

    00001000

    to get:

    11010100
    00001000
    --------------
    00000000

    which indicates that the bit was not set.

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Location
    Honduras
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Helph me !

    I want to generate a mask by means of the position as you aria ? For example if you control the position 3 that I send the 00000100 in order to make a &

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Helph me !

    Are the digits in this String the positions you want: 87654321
    If those are the positions, how many bits does a value (say 1) have to be shifted left to get to position 1?
    How many to position 2? etc
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Helph
    By JosselynMedina in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2014, 11:48 AM