Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of a statement by its result [closed]

Tags:

I know, 42 is the answer to everything but how the heck is 42 the result of this?

int x = -(~'+'|0xAB^1337); //42 
like image 983
Toshi Avatar asked Jun 02 '17 12:06

Toshi


People also ask

What accounts are closed at the end of the accounting period?

Temporary accounts include revenue, expenses, and dividends, and these accounts must be closed at the end of the accounting year.

What is a closing entry example?

For example, a closing entry is to transfer all revenue and expense account totals at the end of an accounting period to an income summary account, which effectively results in the net income or loss for the period being the account balance in the income summary account; then, you shift the balance in the income ...

What is a closing process in accounting?

What is the Closing Process? The Closing Process is a step in the accounting cycle that occurs at the end of the accounting period, after the financial statements are completed. This serves to get everything ready for the next year.

What are the accounts to be closed?

The temporary accounts get closed at the end of an accounting year. Temporary accounts include all of the income statement accounts (revenues, expenses, gains, losses), the sole proprietor's drawing account, the income summary account, and any other account that is used for keeping a tally of the current year amounts.


2 Answers

To understand what happens, you should look at the binary representation of values in this expression. But before that let's look how characters are converted to integers and what bitwise operators are used here.

'+' character is equivalent to integer value 43 - you can check it by casting it to the integer, or by using Convert.ToInt32('+').

0xAB is a hexadecimal literal, which represents decimal value 171.

And the last thing you should understand here is bitwise operators:

  • ~ bitwise NOT - operator looks at the binary representation of the values of the expression and does a bitwise negation operation on it. Any digit that is a 1 in the expression becomes a 0 in the result. Any digit that is a 0 in the expression becomes a 1 in the result.
  • | bitwise OR - looks at the binary representation of the values of two expressions and does a bitwise OR operation on them. Any time either of the expressions has a 1 in a digit, the result will have a 1 in that digit. Otherwise, the result will have a 0 in that digit.
  • ^ bitwise XOR - looks at the binary representation of the values of two expressions and does a bitwise exclusive OR operation on them. When one, and only one, of the expressions has a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.

Now all expression looks like -(NOT(43) OR (171 XOR 1337)). Let's move to binary representation of these numbers and operations:

                          Binary | Decimal | Comments  00000000000000000000000000101011 |      43 | '+' 11111111111111111111111111010100 |     -44 | NOT 43         // bits are inverted  00000000000000000000000010101011 |     171 | 0xAB 00000000000000000000010100111001 |    1337 |  00000000000000000000010110010010 |    1426 | 171 XOR 1337   // different bits produce 1  11111111111111111111111111010100 |     -44 | NOT 43 00000000000000000000010110010010 |    1426 | 171 XOR 1337 11111111111111111111111111010110 |     -42 | -44 OR 1426    // 0 only if both bits are 0  00000000000000000000000000101010 |      42 | -(-42) 

Try online.

like image 200
Sergey Berezovskiy Avatar answered Oct 08 '22 06:10

Sergey Berezovskiy


  1. Evaluate ~'+', which is binary NOT on the ascii value of '+' => -44
  2. Evaluate 0xAB^1337 (binary XOR) => 1426
  3. Evaluate -44|1426 (binary OR) => -42
  4. Evaluate -42 (arithmetical negation) => 42
like image 36
adjan Avatar answered Oct 08 '22 07:10

adjan