Converting C expression to PB

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Max..

Code: Select all

bIDCmd = (VersionParams.bIDEDeviceMap >> i & 0x10) ? \				IDE_ATAPI_ID : IDE_ID_FUNCTION;
I'm having a hard time with this one. Structure is there, constants defined. >> i & 0x10 _seems_ to be clear (at least it looks like the parts where I used that works) but with "? \ ..." the pain starts...

Anywhere Santa's little helpers around? :wink:

Max.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

\ means that the line continues to the next line

expresion ? result1 : result2

is an operator which returns result1 if expression is true
otherwise returns result2

hope that helps :)

Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Max..

Code: Select all

bIDCmd = (VersionParams.bIDEDeviceMap >> i & 0x10) ? IDE_ATAPI_ID : IDE_ID_FUNCTION;
Yes, thanks, helps already.

Found that "? :" are conditional operators. But still I need some digging to understand that... or do you know? :wink:

Max.

Edit:

Found this now...
`?:'
The ternary operator. `A ? B : C' can be thought of as: if A
then B else C. A should be of an integral type.
at http://docs.freebsd.org/info/gdb/gdb.in ... ators.html

Getting closer... :)

PS: Actually you said that already. Sorry, too tired. :-/
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

This is called a conditional expression and used lots in C++ and hibrids (Javascript, etc...) basically you have an expression to evaluate then 2 statements which are executed weather the expression is true or false. As christos says, it usually like this:

var = (expression to evaluate) ? var is this if true : var is this if false;

and thats it :)

--Kale

In love with PureBasic! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.
Found that "? :" are conditional operators. But still I need some digging to understand that... or do you know? :wink:
i believe this code is as this PB (but its been some time since i have touch C so ...:) )

If (VersionParams\bIDEDeviceMap >> i & 0x10)
bIDCmd = #IDE_ATAPI_ID
Else
bIDCmd = #IDE_ID_FUNCTION
endif

Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Max..

If (VersionParams\bIDEDeviceMap >> i & 0x10)
bIDCmd = #IDE_ATAPI_ID
Else
bIDCmd = #IDE_ID_FUNCTION
endif
Yes, beside changing 0x10 to 16 I used it that way and it seems to do. Sadly I will only really be sure once all is done.

As I said yesterdays, was way too tired to understand your posting in the first run though it contained anything needed. The second run was better; must have been the Vodka that helped. :wink:



Max.
Post Reply