Pages: [1]   Go Down
Author Topic: Bitshift question.  (Read 486 times)
0 Members and 1 Guest are viewing this topic.
Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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.
« Last Edit: September 14, 2012, 01:43:38 am by Nick Gammon » Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1728
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
Code:
if((i << (0x20 - PID)) == SupportedPID20){
   //Something here.
}

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

« Last Edit: September 13, 2012, 04:37:28 pm by Tom Carpenter » Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for not being clear.

Quote
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.

Quote
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.

Code:
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.
« Last Edit: September 14, 2012, 01:11:46 am by Pavilion1984 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19026
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes and it's this part below mate.

Code:
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.

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


Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Save us all some pain
...and don't put semicolons where semicolons are not wanted.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: