-2

Actually I know the declaration of array in java but here I am unable to understand the use of this syntax:

int a[] = new int[3 << 1];

In place of a size for the array, here << this symbol is used for?

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Try printing `a.length` and you will see `6` although I don't know why would you want to do it instead of just `6`. `<<` is called signed left-shift operator. See [here](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html) – SomeDude Aug 15 '20 at 14:40
  • It is also in C compatible style: `int[] a = new int[3 << 1];` you will find more often. `3 << 1` (6) maybe stems from a 2x3 matrix being stored in a linear vector of 6 elements. Check the rest of the code – Joop Eggen Aug 15 '20 at 14:41
  • how you people found the 6 i want the process which u used – SUCHITRA GIRI Aug 15 '20 at 14:46
  • `3` is `11b` in binary, shift it by `1` bit to the left, and you get `110b` or `6` decimal. – Johannes Kuhn Aug 15 '20 at 14:47
  • @SUCHITRAGIRI Read [this answer](https://stackoverflow.com/a/141873/1410303) to know more about bit shift operators in Java. – SomeDude Aug 15 '20 at 14:49

4 Answers4

0

<< is the left shift operator and belongs to the bitwise and bit shift operators. For a detailed explanation of how each bitwise and bit shift operator works, please refer to this answer by @DerekPark. Thus, 3 << 1 is an arthmetic expression that gets evaluated before the array is created. 3 << 1 evaluates to 6, thus an int[] of size 6 gets created.

Ideone Demo

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Can you explain me how you got the answer which is 6 , i want the explanation elaborately! – SUCHITRA GIRI Aug 15 '20 at 14:45
  • @SUCHITRAGIRI please read the site I have linked. It explains what the operator does. It basicalls shifts the bit pattern of `3` (which is `0...011` in base2) `1` to the left, filling up a `0` on the right. Thus `0...011` becomes `0...0110`, which, when converted to base10, is `6`. – Turing85 Aug 15 '20 at 14:47
  • @SUCHITRAGIRI I updated my answer and added a linkt to another answer, explaining all bitwise and bit shift operations. – Turing85 Aug 15 '20 at 14:58
0

The << symbol is known as the "left shift operator." As you can probably guess, it gets its name from shifting bit positions to the left.

There's a good example posted by another user at the following:

How do shift operators work in Java?

0

Right shift and left shift operators shift your number to the right of left in their binary representation. If you left shift by one the number 3 3<<1 this would mean that:

The binary for 3 which is 011 gets shifted to the left(basically a 0 is added in the front) 0110 and now your number is 6.

The opposite thing would happen with right shift: If you have the number 3 011 and you right shift it by 1 3>>1 the first bit will get chopped off 01 and you'll end up with the number 1.

Shift operators are useful for multiplying or dividing by powers of 2, and it's a bit faster than just saying 3*2.

JeroSquartini
  • 347
  • 1
  • 2
  • 11
0

<< is the bitwise left shift operator and is often used for efficiently multiplying for specific powers of two, as bitwise operators tend to be faster. x << n is usually equivalent to x*2n, except for cases involving int overflow.

3 in binary is represented as 11 and after shifting to the left by one, it becomes 110, which is 6 in decimal. It can be seen intuitively that shifting by n places to the left is equivalent to multiplying by two n times, as each shift adds a binary digit to the right, which increases the value of every other digit by a factor of 2.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80