Go Down

### Topic: Bitshift question. (Read 714 times)previous topic - next topic

#### Pavilion1984

##### Sep 13, 2012, 10:55 pmLast Edit: Sep 14, 2012, 08:43 am by Nick Gammon Reason: 1
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: [Select]
`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.

#### Tom Carpenter

#1
##### Sep 13, 2012, 11:33 pmLast Edit: Sep 13, 2012, 11:37 pm by Tom Carpenter Reason: 1
I'm not sure I quite follow, but the statement:

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

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

~Tom~

#### GoldStar611

#2
##### Sep 13, 2012, 11:35 pm
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?

#### AWOL

#3
##### Sep 13, 2012, 11:46 pm
there's a very important 'f' missing from your subject.
"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.

#### PeterH

#4
##### Sep 13, 2012, 11:52 pm

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.
I only provide help via the forum - please do not contact me for private consultancy.

#### Pavilion1984

#5
##### Sep 14, 2012, 08:01 amLast Edit: Sep 14, 2012, 08:11 am by Pavilion1984 Reason: 1
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: [Select]
`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.

#### Nick Gammon

#6
##### Sep 14, 2012, 08:47 am

Yes and it's this part below mate.

Code: [Select]
`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: [Select]
`    if(i << (0x20 - PID) & SupportedPID20);    return true;   // <----------- this is always executed`
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Pavilion1984

#7
##### Sep 14, 2012, 09:06 am
PID is a byte that may contain anything from 0x01 to 0x20 and SupportedPID20 is a unsigned long type.

#### PeterH

#8
##### Sep 14, 2012, 03:23 pm

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.
I only provide help via the forum - please do not contact me for private consultancy.

#### AWOL

#9
##### Sep 14, 2012, 03:27 pm
Quote
Save us all some pain
...and don't put semicolons where semicolons are not wanted.
"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.

Go Up

Please enter a valid email to subscribe