Converting bool expression to char in c#

前端 未结 3 2015
旧巷少年郎
旧巷少年郎 2021-02-07 01:00

I passed .NET quiz, when I met a question like below one.

Char ch = Convert.ToChar(\'a\' | \'e\' | \'c\' | \'a\');

In console we can see that o

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 01:23

    Go to unicode-table.

    • 'a' Decimal value is 97 in binary it's 01100001.
    • 'e' Decimal value is 101 in binary it's 01100101.
    • 'c' Decimal value is 99 in binary it's 01100011.
    • 'a' Decimal value is 97 in binary it's 01100001.

    Or operator in bit wise is '|'. So your expression is equal to:

    01100001 OR
    01100101 OR
    01100011 OR
    01100001 and the result for this is
    01100111.

    Or results 1 if there is at least one time 1 in the column.

    01100001 converting to Decimal is 103.
    We will go again to the unicode-table and we will see that 103 in deciaml is equal to 'g'.

    So you asked what that function does, it calculates the binary value then converts it to Decimal value and returns the unicode character of it.

提交回复
热议问题