-2

The code I am referring to occurs here. I did not know that this was legal c that does not result in a segfault. what does it mean? is it equivalent to var = &var[3]?

skyfire
  • 227
  • 1
  • 6
  • There are no pointers here, `pos` is an `int4` (probably a typedef for `int32_t` or the like). It just adds 1 to `var`, ands the result with `3` (effectively finding the remainder mod 4), and stores the result back in `var`. What else did you think it did? – Nate Eldredge May 21 '20 at 17:02
  • The `pos` variable is an integer declared as `int4`, not a pointer. Why would you expect a segfault due to arithmetic operation on integers? – CiaPan May 21 '20 at 17:02
  • 2
    Perhaps you are mixing up the unary operator `&` (address-of) with the binary operator `&` (bitwise and)? If it were the unary `&` the expression would not be valid; there's no meaning in C for two expressions next to one another (in particular it's not multiplication). – Nate Eldredge May 21 '20 at 17:02
  • 1
    Why do you modify the question to make it inconsistent with the code you're refering to? – CiaPan May 21 '20 at 17:06
  • Post the relevant code here. – chux - Reinstate Monica May 21 '20 at 17:49

1 Answers1

2

Nope. The expression pos = (pos+1)&3; is equivalent to

pos = pos + 1;
pos = pos & 3;

except it does not imply storing the intermediate value back to the pos variable.

EDIT to reply the additional question in a comment

The bitwise AND operator truncates, or rather wraps, the incrementing process at the value of 3. The code could be written as

pos = pos + 1;
pos = pos % 4;

which in this context means:

pos = pos + 1;
if( pos > 3 )
    pos = 0;

However, the arithmetic modulus modulo (thanks, @chux - Reinstate Monica) or the bitwise AND expressions make the same result in fewer operations, most important without a conditional branch, which would downgrade the efficiency.

Compare the question Why is processing a sorted array faster than processing an unsorted array? and this answer to it.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • Ah I was being a dummy, Read the ```&``` and immediately thought ```pointer```. Thanks. – skyfire May 21 '20 at 17:15
  • 1
    Do you know why a position variable would be logical AND'd with 3? – skyfire May 21 '20 at 17:19
  • Nvm here it is so that ```pos``` never reaches 4 as ```4 = 0100``` and ```pos``` sets the value of ```lookahead[4]```. Taking the AND ensures that pos never reaches out-of-bounds in ```lookahead``` – skyfire May 21 '20 at 17:30
  • Wrong. Integer constants starting with `0` are considered _octal_ in C, so `0100` is `64`, not `4`. – CiaPan May 21 '20 at 17:50
  • `pos = pos % 4;` differs from `pos = pos & 3;` when `pos < 0`. I see no initialization of `pos` preventing `pos < 0` unless it is buried in the C++ `int4`. `%` is a remainder operator, not a Euclidean modulus. – chux - Reinstate Monica May 21 '20 at 17:59
  • @chux-ReinstateMonica Nothing is 'buried in C++' here, it is explicitly written in the code. See the `XmlScan` constructor for initialization of `pos` to zero. – CiaPan May 21 '20 at 18:25
  • @chux-ReinstateMonica You're right, I meant 'modulo' but typed too fast and didn't check before submitting. – CiaPan May 21 '20 at 18:27
  • Er - I meant `modulo` too. `%` is a remainder operator, not a Euclidean modulo. Results differ with `a%b` when `a < 0` - which appenly does not apply in _this_ code. – chux - Reinstate Monica May 21 '20 at 18:34
  • @CiaPan I'm confused how you are getting octal from ```int4 pos = 0```? 0 would be interpreted as an integer... – skyfire May 21 '20 at 18:54
  • I think you meant if ```0100``` were in the src code because then you would have an octal number. But I was referring to the byte representation. – skyfire May 21 '20 at 18:59
  • @skyfire You wrote `4 = 0100`. It's clear to me you meant a binary representation here, but you didn't say that. And when we talk about the program in C/C++ we should comply to C/C++ syntax – and it defines a constant starting with digit `0` as octal (see e.g. [_C - Constants and Literals_](https://www.tutorialspoint.com/cprogramming/c_constants.htm) at TutorialsPoin.com), hence `0100` is 8 squared, i.e. `64` in decimal. – CiaPan May 21 '20 at 19:01