wittrup
wittrup

Reputation: 1565

Reading c# if statement

Converting C# to Pascal i stumble upon this line:

if (bb[1] == '1'-'0')

What does it mean? If second byte is equal to string '1' minus string '0'?

Upvotes: 2

Views: 227

Answers (3)

skolte
skolte

Reputation: 367

watch window in visual studio

'1' is converted to decimal which corresponds to 49, '0' as decimal gives 48. 49 - 48 = 1. So '1' - '0' is 1 as shown in the watch window. Here's ASCII character set if you have other similar comparisons.

Upvotes: 0

Matt Richard
Matt Richard

Reputation: 180

C# doesn't have single-quoted strings, so those are both characters (type char).

Values of type char are represented in memory as the integer value of their ASCII code, so some limited math operations work on them (like subtraction in your example).

'1' - '0' is the same as (int)'1' - (int)'0' which is 49 - 48.

Upvotes: 0

Habib
Habib

Reputation: 223257

'1'-'0' this would give you the 1 as integer value. It is like converting character to respective integer number.

It is same as:

int value = (int)Char.GetNumericValue('1');

where value will hold 1

so your check is:

if(bb[1] == 1)

Upvotes: 3

Related Questions