Bitshift question.

Hello again.

Could someone please tell me if i am correct in thinking if that Supported_PID20 has 00010000000000000000000000000000 in it and unsigned long i has (well think it should have if my code is correct) 00010000000000000000000000000000 in it the if statment will be executed?

byte Check_Supported_PID(byte PID)
{
  unsigned long i = 1;
  
  if(PID == 0x00)
  {
    return true;
  }
  else if(PID >= 0x01 && PID <= 0x20)
  {
    if(i << (0x20 - PID) & SupportedPID20);
  }

Cheers ppl.

I'm not sure I quite follow, but the statement:

if((i << (0x20 - PID)) & SupportedPID20){
   //Something here.
}

If say for example Supported_PID20 is xxx1xxxxxxxxxxxxxxxxxxxxxxxxxxxx, then the statement would execute if PID = 0x04, where x is don't care.

The following modified statement:

if((i << (0x20 - PID)) == SupportedPID20){
   //Something here.
}

If say for example Supported_PID20 is 00010000000000000000000000000000, then the statement would execute if PID = 0x04.

if that Supported_PID20 has 00010000000000000000000000000000 in it and unsigned long

That is a VERY large number, much larger than can fit into a unsigned long. Is that decimal or binary representation?

there's a very important 'f' missing from your subject.

Pavilion1984:
Hello again.

Could someone please tell me if ... the if statment will be executed?

Can you modify that code to make it more obvious which block of code you're interested in having executed? There are three 'if' statements, two of which have an associated code block, and it's not obvious which of these five things you're talking about.

Sorry for not being clear.

That is a VERY large number, much larger than can fit into a unsigned long. Is that decimal or binary representation?

This is the binary representation of the number.

Can you modify that code to make it more obvious which block of code you're interested in having executed? There are three 'if' statements, two of which have an associated code block, and it's not obvious which of these five things you're talking about.

Yes and it's this part below mate.

else if(PID >= 0x01 && PID <= 0x20)
  {
    if(i << (0x20 - PID) & SupportedPID20);
    return true;
  }

The SupportedPID20 variable (decleared at the top of my code) will contain something like 11111111001111110001010111110111 binary (when running on a car) and lets say i variable will contain something like 00010000000000000000000000000000 binaray (if PID = 0x04). Am wanting to compare 1 bit so if bit 4 of SupportedPID20 is 1 and bit 4 of i is 1 will the if statment return true? Well thats what i want it to do anyway lol.

Pavilion1984:
Yes and it's this part below mate.

else if(PID >= 0x01 && PID <= 0x20)

{
   if(i << (0x20 - PID) & SupportedPID20);
   return true;
 }

What data type is SupportedPID20?

These code snippets don't really help. Not knowing what type PID and SupportedPID20 are makes it hard to reply.

Also that semicolon shouldn't be there.

    if(i << (0x20 - PID) & SupportedPID20);
    return true;   // <----------- this is always executed

PID is a byte that may contain anything from 0x01 to 0x20 and SupportedPID20 is a unsigned long type.

Pavilion1984:
PID is a byte that may contain anything from 0x01 to 0x20 and SupportedPID20 is a unsigned long type.

Save us all some pain.

Write a sketch that declares and initialises the variables you're using and then executes the code in question. Design the sketch to print something out when the right thing happens, and to print something different if the wrong thing happens. Tell us what happens when you run the code.

Save us all some pain

...and don't put semicolons where semicolons are not wanted.