0

This is the algorithm to print PowerSet From a Set

Input: Set[], set_size

1. Get the size of power set
   powet_set_size = pow(2, set_size)

2  Loop for counter from 0 to pow_set_size
     (a) Loop for i = 0 to set_size

          (i) If ith bit in counter is set
              Print ith element from set for this subset
     (b) Print seperator for subsets i.e., newline

Can anyone explain me what it means by If the ith bit in counter is set.

Thanks :)

3
  • What do you know about binary? Commented Feb 25, 2015 at 7:57
  • Binary is a number representation in base 2 .. Commented Feb 25, 2015 at 8:59
  • A digit in binary is called a bit. Bits are numbered right to left, so if the i'th bit is set, that means that the i'th digit (right-to-left) is equal to one. Commented Feb 25, 2015 at 9:02

1 Answer 1

1

A number is in a computer represented in binary notation. For instance 5 is represented as:

00000000101

Because the zero'th bit is one and so is the second. Thus 2^0+2^2=5.

You can for instance test whether the i-th bit is "set" (meaning it is equal to one), using the following test:

(n&(1<<i)) != 0
  1. You first shift 1 i places to the left. Such that it creates a number where only the i-th bit is set.
  2. Next you perform a bitwise and such that the result differs from zero if and only if that bit is set for n as well.
  3. Finally you perform that check.

If you count in binary, eventually all possible combinations of zero's and one's will be enumerated:

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.