-2

Consider the i-th column of a binary matrix is denoted with matrix[i]. Let D be the number of columns of the matrix.

My question: What is the result of the following code. In fact, I can't understand the role of the 1llu expression.

matrix[i]^((1llu << D)-1)
Simson
  • 3,373
  • 2
  • 24
  • 38
user0410
  • 145
  • 7
  • 2
    `llu` is a suffix for `unsigned long long int` – nada Oct 22 '19 at 07:52
  • Possible duplicate of [LLU bad suffix on number](https://stackoverflow.com/questions/14588997/llu-bad-suffix-on-number) – phuclv Oct 22 '19 at 08:10
  • 1
    Possible duplicate of [What is 1LL or 2LL in C and C++?](https://stackoverflow.com/questions/16248221/what-is-1ll-or-2ll-in-c-and-c) – John Oct 22 '19 at 12:08

2 Answers2

2

This has to be looked at from the binary representation.

1llu means 1 represented as a unsigned long long.

...0000 0000 0000 0001

<< D shift that 1 left D amount of times (bits)

If D==5 then :

...0000 0000 0010 0000

- 1 subtract 1 from the shifted result ( which gives 1's on the positions 0 ~ D-1)

...0000 0000 0001 1111

The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

https://learn.microsoft.com/en-us/cpp/cpp/bitwise-exclusive-or-operator-hat?view=vs-2019

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
1

It is easy to explain with an example below:

Example 1: 1 << 34
Example 2: 1llu << 34

If the integer size if 32 bits, then Example 1 would produce 0x0000 0000 as 1 will fall off. Whereas, Example 2 would produce 0x0000 0004 0000 0000

So, it should be seen with the context of the what is the type / size of matrix element.

rush2shri
  • 21
  • 1
  • The basic idea is true, but if the integer size is 32 bits, 1<<34 invokes undefined behavior. In fact, if naively done on x86/x64, the shift count will wrap around, producing 4 as a result. – Sneftel Sep 30 '20 at 12:03