1

I don't understand what is this doCalculatePi means or does, in the following example:

  public static double doCalculatePi(final int sliceNr) {
    final int from = sliceNr * 10;
    final int to = from + 10;
    final int c = (to << 1) + 1;
    double acc = 0;
    for (int a = 4 - ((from & 1) << 3), b = (from << 1) + 1; b < c; a = -a, b += 2) {
        acc += ((double) a) / b;
    }
    return acc;
}

public static void main(String args[]){
    System.out.println(doCalculatePi(1));
    System.out.println(doCalculatePi(2));
    System.out.println(doCalculatePi(3));
    System.out.println(doCalculatePi(4));
    System.out.println(doCalculatePi(10));
    System.out.println(doCalculatePi(100));
    }

I have printed the values to understand what the results are but I still have no clue what this code calculates. The conditions inside the loop are not clear.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Bionix1441
  • 2,135
  • 1
  • 30
  • 65
  • 7
    You might want to take a look at the [Operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) page of the java Tutorials. – azurefrog Jun 22 '15 at 18:27
  • 1
    " I still have no clue what this code calculates." OK, then run it and see what it calculates. – nanofarad Jun 22 '15 at 18:28
  • I did but the output is not evident – Bionix1441 Jun 22 '15 at 18:30
  • 1
    Possible dupe of http://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work?rq=1 – Louis Wasserman Jun 22 '15 at 18:37
  • 1
    You should elaborate on what is "not clear" and "not evident". What, ***exactly***, are you asking? Many here are assuming you don't know what `<<` means, but you haven't actually said that that is the baffling part. – dcsohl Jun 22 '15 at 18:38
  • Oh, I see now the title does ask about `<<`. Still, the rest of the question is vaguely worded at best... – dcsohl Jun 22 '15 at 18:45
  • 1
    See the link for help :). [link] (http://www.dotnetperls.com/shift) – Waqas Shabbir Jul 23 '15 at 05:14

3 Answers3

10

<< means left shift operation, which shifts the left-hand operand left by the number of bits specified by the right-hand operand (See oracle docs).

Say, you have a decimal value, 5 which binary representation is 101

Now for simplicity, consider,

byte a = (byte)0x05; 

Hence, the bit representation of a will be,

a = 00000101 // 1 byte is 8 bit

Now if you left shift a by 2, then a will be

a << 2
a = 00010100 //shifted place filled with zero/s

So, you may now understand that, left shift a by 3 means

a << 3
a = 00101000

For better understanding you need to study Bitwise operation.

Note, you are using int instead of byte, and by default, the int data type is a 32-bit signed integer (reference here), so you have to consider,

int a = 5;

in binary

a << 3
a = 00000000 00000000 00000000 00101000 // total 32 bit
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
4

My guess is that it approximates PI with

PI = doCalculatePi(0)+doCalculatePi(1)+doCalculatePi(2)+...

Just a guess.

Trying this

double d = 0;
for(int k = 0; k<1000; k++) {
    System.out.println(d += doCalculatePi(k));
}

gives me

3.0418396189294032
3.09162380666784
3.1082685666989476
[...]
3.1414924531892394
3.14149255348994
3.1414926535900394
NaN
  • 7,441
  • 6
  • 32
  • 51
  • I agree with this answer... not sure why so many downvotes... (Oh look, vindication.) – River Jun 22 '15 at 18:43
  • 1
    I didn't downvote, but one flaw with the answer is that it fails to address OP's main issue: not knowing what the `<<` operator does. There's also no explanation of _why_ the code should generate the value of pi. Also, OP's code does not sum the return values of calls to `doCalculatePi()` with different arguments; it just prints the values, so it's hard to understand the relationship between this answer and OP's question. – Ted Hopp Jun 22 '15 at 18:44
  • 1
    I mean, he could throw in a link to the Java operators page, but the `<<` issue is pretty basic and can be looked up on Google. This answers the part of the question that is the reason it was asked on SO. (Though I suppose a more accurate answer than "converges PI" would be "calculates the xth 10 digits of PI".) – River Jun 22 '15 at 18:47
1

<< is the Bitshift operator.

Basically, every number is represented as a series of binary digits (0's and 1's), and you're shifting each of those digits to the left by however many places you indicate. So for example, 15 is 00001111 and 15 << 1 is 00011110 (or 30), while 15 << 2 is (00111100) which is 60.

There's some special handling that comes into play when you get to the sign bit, but you should get the point.

Matt
  • 11,523
  • 2
  • 23
  • 33