0

I have made a program in which i want to calculate the power set size.But i am unable to do it because the ouput of the statement 1 is 0?Why is this showing wrong?

public class PowerSetDemo 
{
    public static void main(String s[])
    {

     int q=0;
     char a[]={'a','b'};
     int p=a.length;
     System.out.println(p);

     q=2^p;        //error here
     System.out.println(q);    Why 0?

    }
}

OutputShown 2 0

2 Answers2

2

Use Math.pow not ^.
^ is a bitwise operation, as mentioned here

Refer to Math.pow for more information.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
  • I want to calculate the length of this set by using my own logic first.Then i will use those predefined functions to make my life easy.Is there any other way i can calculate the length of the no of combinations? – mansi kbchawla Jun 03 '15 at 03:43
1

p is the length of the array which is 2.

When you xor 2 with 2 (or any other number with itself) you get zero:

2^2 = 0
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Okay in java its xor.While using calculator i thought may be i can calculate the length of the no combination of powers.Is there any other way i can calculate the length of the no of combinations? – mansi kbchawla Jun 03 '15 at 03:43
  • @mansikbchawla as Gosu suggested - use `Math.pow` to calculate power. As for combinations - I'm not familiar with a library that does it for you, but you can easily [google it](http://introcs.cs.princeton.edu/java/23recursion/Combinations.java) – Nir Alfasi Jun 03 '15 at 03:46