-3

What does this code mean?

int possible = 1;
for (int i = 0; i < n - 1; ++i){
   possible += (n_size[i] ^ n_size[i + 1]) < 0;
}

I think this is ^ XOR, but how is it working in this code? That's strange,

Because I thought when we use XOR we have just 0 or 1. Please, help me to understand.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Brian
  • 1
  • 2
  • 5
    bitwise xor....And no...you were thinking wrong..`100^11 = 111` neither `0` nor `1` – user2736738 Dec 28 '17 at 11:37
  • 1
    Where did you find this? I don't get it. That expression is only going to be less than zero if only one of the operands of the exclusive OR has its negative sign bit set. What's the use of this? – Zebrafish Dec 28 '17 at 11:53
  • @Zebrafish You literally just explained its use – Passer By Dec 28 '17 at 11:54
  • 1
    @Brian Consider reading a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Passer By Dec 28 '17 at 11:54
  • @Passer By So it counts the number of times any two consecutive values in an array have differing sign bits? Is there a use for this? Where did you find this? – Zebrafish Dec 28 '17 at 11:58
  • this n_size[] is a array with numbers between 0 and 99999. How do I know if I use this operator I you get the answer? I know 0^1 = 1, 0^0=0 ... but I don't get it yet. What book can help me? – Brian Dec 28 '17 at 12:01
  • @Zebrafish Off the top of my head, count the number of inversions? Why is this even an issue? – Passer By Dec 28 '17 at 12:03
  • You *do* have 0s and 1s, just possibly 32 of them at the same time. – Bo Persson Dec 28 '17 at 12:17
  • This is rather bad code. Understanding how `^` works here is worth investigating. Figuring out how to use this code in the real world is not; better to spend time figuring out how to do it better, i.e., **clearly**, so that readers don't have to ask what's going on. – Pete Becker Dec 28 '17 at 14:40
  • Note that if the array `n_size` is an unsigned type or only contains positive values (or only contains negative values), the loop body won’t increment `possible`. – Jonathan Leffler Dec 28 '17 at 15:47

2 Answers2

1

Let's see this line :

possible += (n_size[i] ^ n_size[i + 1]) < 0;

We don't know about n_size but I'll suppose it is an array of n int. So we XOR (bitwise) two consecutive terms of n_size, and determine the sign of the result (by comparing it to 0).

Bitwise-XOR is operating bit per bit, so (ABCD = 1011 ^ 0101 <=> A = 1 ^ 0 , B = 0 ^ 1, C = 1 ^ 0, D = 1 ^ 1).

int are encoded in a certain manner, which allows us to get the sign with the most-significant bit (in the number 0bX???????, if X=1 the number is negative, else the number is positive).

So (A ^ B) < 0 is equivalent to (A < 0) ^ (B < 0).

So this line increments possible when two consecutive terms have not the same sign.

Finally, possible counts the number of consecutive terms alterning their sign.

PS : notice that float and double have their sign determined by their most-significant-bit too, so it works the same if n_size is an array of float or double.

Julien Vernay
  • 295
  • 3
  • 13
  • This is what I was looking for, "possible" increments when two consecutive terms don't have the some sign. So, can I understand this like IF or ELSE? I'm trying to say, " if(consecutives not some sign) then increments - else (do nothing). Is it a way to see this? – Brian Dec 28 '17 at 12:17
  • @Brian yes, because true = 1 and false = 0, so "possible += boolean" means : "if (boolean) possible++;" – Julien Vernay Dec 28 '17 at 12:22
0

As coderedoc was a little short in his comment: ^ is a bit-wise operator, just as | and &, too. Those operators are applied on every pair of corresponding bits (and for such a pair, XOR as you understood it) within two variables:

unsigned char c1 =                0b11001010;
unsigned char c2 =                0b10101100;
unsigned char c3 = c1 ^ c2; // == 0b01100110
unsigned char c4 = c1 & c2; // == 0b11101110
unsigned char c5 = c1 | c2; // == 0b10001000
Aconcagua
  • 24,880
  • 4
  • 34
  • 59