Thread: Is this considered an AND operation?

  1. #1
    Unregistered
    Guest

    Question Is this considered an AND operation?

    I am a super newbie to C, but have 3 years experience with VB. I am converting a C program to VB and have run into something I am not familiar with. Here is the code. My question is below the code:

    Code:
    status1 = inp(UUT_baseaddr+1) & 0x7F;
    status2 = inp(UUT_baseaddr) & 0xFF;
    After the call to the 'inp' routine there is the '&' symbol and a HEX value. They come out to be 127 and 255.

    What exactly is that doing?

    Is is concactenating 127 and 255 to the result from 'inp'?

    When I use this command in VB:

    Code:
    status1 = inp(UUT_baseaddr+1) & 127
    I get the response from inp as 255, but then VB concactenates the 127 to the end of the response and I end up with

    "255127"

    Which I don't think it is correct.

    Any help or tips is greatly appreciated. I am a C newbie so your comments are greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    The hex values are masks, forcing the values to 0 in case of FF and 128 in the case of 7F. (Provided the values are bytes).

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For the record, yes, it is an 'and' symbol. It is a bitwise and, and not a logical and.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    I see. So if it were to return 255 or 127, then the program would make them equal to zero?

    Thanks again for your expertise and help!


  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    & is the bitwise AND operator.

    In other words:
    status1 = inp(UUT_baseaddr+1) & 0x7F;
    status1 would contain the lower 7 bits of inp(UUT_baseaddr+1)


    status2 = inp(UUT_baseaddr) & 0xFF;
    status2 would contain the lower 8 bits of inp(UUT_baseaddr)

    I'm assuming your result is a 16-bit number with status1 being the most significant. Therefore youre result would be the lower 15 bits.

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I see. So if it were to return 255 or 127, then the program would make them equal to zero?
    NO it would not because

    255 which is:
    0xFF
    AND
    0xFF
    would result in 0xFF

    If your function returned 0xFF and you & with 0x7F, you would get 0x7F. Dunno where you guys are getting the zero from.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    Ok. I was a little confused at first.

    By using the '&' I am specifying which bits I want to read from the result and assigning them to the variable status1 or status2?

    Am I on the right track here??

    Thanks to everyone for your help so far.

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Yep, that's what the & operator is primarily used for.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go here for a good tutorial.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    Thanks so much!! It is working great now. Your response was exact!!

    Thanks again!!



Popular pages Recent additions subscribe to a feed