-1

I have come across a line of code.

int m_iCorners = 30 , m_iTesselation2 = 10;
int iNeededSize = (4 + (m_iCorners + 1) * (m_iTesselation2 << 1));

what is the role of << operator in the code above and how does it affect the m_iTesselation2 value?

Waqar
  • 8,558
  • 4
  • 35
  • 43
shomit
  • 209
  • 1
  • 7

3 Answers3

4

<< is the left shift operator. It shifts the bits of a number to the left by the number given on the right side of the operator. Example:

// Showing only 8 bits here for simplicity (int is 32 bits wide).
int x = 10;    // bits: 0000 1010
int y = x << 1 // bits: 0001 0100 (bits shifted by 1 to the left)
y = x << 2;    // bits: 0010 1000 (bits shifted by 2 to the left)

Shifting an int to the left by 31 is undefined behaviour. See https://godbolt.org/z/8ShJ9u

y = x << 31; // !!( THIS IS UB, thanks @Ted Lyngmo)
y = x >> 31; // This is fine, y will be 0

Note than x itself is not changing here, a temporary is created and the value is assigned to y, same as if you had done y = x + 2.

A shift by left is usually a quick way to multiply anything by 2. Shifting 1 bit results in a x * 2. Shifting by 2 bits is equivalent to x * 2 * 2.

Waqar
  • 8,558
  • 4
  • 35
  • 43
2

<< and >> are the shift operators. They shift the value of their first operand to the left/right with the number of bits specified by their second operand.

<<

Let's see how it works for 13 << 3.

The binary representation of 13 is 1101. In computer the value is stored on a fixed number of bits, depending on its type. Let's assume it is stored on 2 bytes in our program.

This is how 13 and 13 << 3 are represented on 2 bytes:

    0000 0000  0000 1101            <--- 13

000 0000 0000  0110 1000            <--- 13 << 3
^^^                  ^^^
 |                    +----- 3 new bits
 +----- these bits are lost

The bits of 13 are shifted 3 positions to the left. The leftmost 3 bits of the original value are lost. Three 0 bits are inserted on the right.

All in all, the value of a << b is (a * 2^b) mod 256^n where n is the number of bytes used to represent the value of a. A left shift works the same as a multiplication with a value that is a power of 2.

>>

The right shift (>>) is similar. The bits are moved to the right, the rightmost bits are lost and new bits are inserted on the left. The bits inserted on the left side depend on the sign of the leftmost operand. If the value of the first operand is negative, the right shift inserts 1 to the left; otherwise it inserts 0.
This behaviour produces a value that has the same sign as the first operand.

The right shift works as an integer division by a number that is a power of 2. The value of a >> b is (a / 2^b).

Important!

Integer promotions are applied to both operands first. 13 in this example is handled as int and on most nowadays architectures this means it is represented on 4 bytes, even if 1 byte is enough to store it.

The behaviour of both << and >> is undefined if the number of bits to shift is negative or larger than the size in bits of the first operand after promotion.

Read more about the shift operators.

axiac
  • 68,258
  • 9
  • 99
  • 134
1

The value of m_iTesselation2 is 10, i.e. 0000 1010 in binary. When you left shift 1, i.e. 0000 0001 in binary, it shifts one bit to 10 in the following syntax:

int iNeededSize = ... (m_iTesselation2 << 1);

Thus you get 0001 0100 which is equivalent to decimal 20.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34