#<< Shift Operator in a power set method and the use of the single &ampersan

72 messages · Page 1 of 1 (latest)

compact raft
#
  static void printPowerSet(int []set, int set_size)    {

        long pow_set_size =  (long)Math.pow(2, set_size);
        int counter, j;

        System.out.println("POWER_SET of: " + Arrays.toString(set));
        System.out.println("Size 2^" + set_size + " = " + pow_set_size);
        /*Run from counter 000..0 to
        111..1*/
        for(counter = 0; counter < pow_set_size; counter++)   {
            System.out.print("{");
            for(j = 0; j < set_size; j++) {
                /* Check if jth bit in the
                counter is set If set then
                print jth element from set */
                if((counter & (1 << j)) > 0) {
                    System.out.print(set[j] + "," );
                }
            }

            System.out.print("}, ");
        }
    }

this was taken from GeeksForGeeks, but the part im trying to understand is this line

if ((counter & (1 << j) > 0 )

i understand the outcome and the left shifting a bit, just from reading ....ie I undertsand 0010 left shifted << becomes 1000 in binary ..which is 8 in decimal
but i'm having a hard time seeing how this if statement is being evaluated with the counter variable.

what i think i know:
the single & means both statements are evaluated regardless if true or false, but i dont see how counter is evaluated alone

fathom scrollBOT
#

This post has been reserved for your question.

Hey @compact raft! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

compact raft
#

this is an example print from that method

#

so counter goes from 0 to 15 ...and j keeps going from 0 to 3

solar marsh
#

Do you know what & does?

compact raft
#

thats the operator that confuses me, i tried looking it up a bit, and what little i gathered was it evaluates both statements regardless if the 1st one is false or true

#

so maybe my question shoulda been about that operator and not the left shift ><

solar marsh
#

& does a bitwise AND

solar marsh
solar marsh
compact raft
#

lol why does it end up 3

#

err 2

#

As a Bitwise AND: & operator is used for adding Bitwise numbers in Java. Bitwise numbers are binary numbers stored in the form of integers

solar marsh
#

& takes two numbers (in binary) and creates another number (in binary) where only bits are 1 if the corresponding bits of both operands are 1

compact raft
#

ooh

#

the 2n'd diget both have 1 ..so its like a matching game lol....

solar marsh
#

yes

#

it takes both numbers and compares the corresponding bits

#

if a specific bit is 1 on both numbers, the same bit will be 1 in the result

compact raft
#

so thats how its using & to create the power function

solar marsh
#

if not, it will be 0

compact raft
#

and the > 0 is if nothing matches ...return false

solar marsh
#

ok so you have 1 << j

#

that will take 1 as a binary number and shift it left by j binary digits

compact raft
#

so 001
010
100

solar marsh
#

if j is 3, then the result of that operation will be 1000 (binary)

#

1<<3=1000 (binary)

#

1<<5=100000 (binary)

#

ok?

compact raft
#

oh so it sets 3 first ,,then shifts left twice?

solar marsh
#

if you do 1<<j, you will get a number where bit j is set to 1 and all other bits are 0

#

Is that clear?

compact raft
#

for the most part =p

solar marsh
#

ok so you have counter & (1<<j)

#

the right side is a number where only a single bit is 1

compact raft
#

so if j is 3 ...is it saying the 3rd binary digit is a 1 ... ( 00100 ) then 1 << shifts that left two places

#

to 10000

solar marsh
#

0001 here, bit 0 is set

#

0010 here, bit 1 is set

#

0100 bit 2 is set

compact raft
#

aah ok ok

#

i follow

solar marsh
#

so all bits except bit j are erased to 0

#

counter & (1<<j) is just extracting bit j from the number counter

compact raft
#

ok so scenaryo
count = 1 , j = 1
(0010 & ( 1 << 0010) > 0 )
== (0010 & (1000) > 0 )
== ( 0 > 0 ) which == false

solar marsh
#

why 0010?

#

1 in binsry is 00000001

#

or 0001

#

the amount of leading zeros doesn't matter

compact raft
#

right, i was just trying to match the digit size, but ok round 2 ...

#

count = 1 , j = 1
(1 & ( 1 << 001) > 0 )
== (1 & (100) > 0 )
== ( 0 > 0 ) which == false

#

i feel like i messed up j

solar marsh
#

you are using << incorrectly

#

a << b
means you take a in binary

#

and move a to the left

#

by b digits

compact raft
#

ooooh snap

solar marsh
#

so 0010100 << 2 = 1010000

solar marsh
compact raft
#

count = 1 , j = 1
(1 & ( 1 << 1) > 0 )
== (1 & (10) > 0 )
== ( 0 > 0 ) which == false

solar marsh
#

essentially << means "add that many 0s go the binary number at the end"

compact raft
#

haha

#

thank u so much, that was buggin me, i used the method for a descrete structures assignment, and i dont like using something i dont understand

fathom scrollBOT
solar marsh
#

and before you get confused about what's decimal and what's binary, add a 0b or similar before all binary numbers

#

then you know what's binary and what isn't xd

#

just a recommendation

compact raft
#

thanks again