What does “”“ if((counter & (1 < 0): ”“” do?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?
if((counter & (1 << j)) > 0):
python python-3.x if-statement data-structures set
add a comment |
I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?
if((counter & (1 << j)) > 0):
python python-3.x if-statement data-structures set
How do we know without knowing what iscounter
,i
andj
?<<
is bitwise left shift operator doc.
– Austin
Nov 25 '18 at 5:43
It checks if bit numberj
incounter
is set.
– Michael Butscher
Nov 25 '18 at 5:49
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33
add a comment |
I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?
if((counter & (1 << j)) > 0):
python python-3.x if-statement data-structures set
I am trying to print all subsets of an array. I came across this if statement in on of the solutions.
What does this if condition do?
if((counter & (1 << j)) > 0):
python python-3.x if-statement data-structures set
python python-3.x if-statement data-structures set
asked Nov 25 '18 at 5:35
I am LI am L
1219
1219
How do we know without knowing what iscounter
,i
andj
?<<
is bitwise left shift operator doc.
– Austin
Nov 25 '18 at 5:43
It checks if bit numberj
incounter
is set.
– Michael Butscher
Nov 25 '18 at 5:49
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33
add a comment |
How do we know without knowing what iscounter
,i
andj
?<<
is bitwise left shift operator doc.
– Austin
Nov 25 '18 at 5:43
It checks if bit numberj
incounter
is set.
– Michael Butscher
Nov 25 '18 at 5:49
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33
How do we know without knowing what is
counter
, i
and j
? <<
is bitwise left shift operator doc.– Austin
Nov 25 '18 at 5:43
How do we know without knowing what is
counter
, i
and j
? <<
is bitwise left shift operator doc.– Austin
Nov 25 '18 at 5:43
It checks if bit number
j
in counter
is set.– Michael Butscher
Nov 25 '18 at 5:49
It checks if bit number
j
in counter
is set.– Michael Butscher
Nov 25 '18 at 5:49
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33
add a comment |
2 Answers
2
active
oldest
votes
Your statement:
if ( (counter & (1 << j)) > 0 ):
It's some bitwise operations. Let's break it down:
(1 << j)
generates the number0b1
and shifts it left byj
places -j
must be an integer. This is akin to saying2**j
, or 2 to thej
th power, but doing it with the bit-shifting operator<<
makes it clear we're doing bitwise operations.
counter & (1 << j)
takes the result of that last operation, and does a bitwiseand
with the variablecounter
. It seems thatj
is a specifier for a bitmask - it tells which bit is important to check in incounter
. Since whatever(1 << j)
produces will only have one1
in its binary representation, the expressioncounter & (1 << j)
will always produce either a power of 2, or 0.
> 0
checks if the number that was produced was 0.
All in all, this is a fairly involved way to check if the j
th bit from the right in counter
is equal to 1
or 0
. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
add a comment |
The & operator is a bitwise operator. More information here.
Basically, if we consider counter as a binary:
counter = 0b0010
1 << j -> with j = 0
j will be the position that you want to evaluate with the 1.
Therefore, in this case the IF statement will not be executed because the AND operator will return 0.
But with:
counter = 0b0010
1 << j -> with j = 1
The IF statement will be executed because the AND operator will return 1.
To understand a little more, you can play with the following piece of code and change the values of counter and j:
counter = 0b0100
j = 2
if((counter & (1 << j)) > 0):
print("True")
else:
print("False")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464944%2fwhat-does-ifcounter-1-j-0-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your statement:
if ( (counter & (1 << j)) > 0 ):
It's some bitwise operations. Let's break it down:
(1 << j)
generates the number0b1
and shifts it left byj
places -j
must be an integer. This is akin to saying2**j
, or 2 to thej
th power, but doing it with the bit-shifting operator<<
makes it clear we're doing bitwise operations.
counter & (1 << j)
takes the result of that last operation, and does a bitwiseand
with the variablecounter
. It seems thatj
is a specifier for a bitmask - it tells which bit is important to check in incounter
. Since whatever(1 << j)
produces will only have one1
in its binary representation, the expressioncounter & (1 << j)
will always produce either a power of 2, or 0.
> 0
checks if the number that was produced was 0.
All in all, this is a fairly involved way to check if the j
th bit from the right in counter
is equal to 1
or 0
. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
add a comment |
Your statement:
if ( (counter & (1 << j)) > 0 ):
It's some bitwise operations. Let's break it down:
(1 << j)
generates the number0b1
and shifts it left byj
places -j
must be an integer. This is akin to saying2**j
, or 2 to thej
th power, but doing it with the bit-shifting operator<<
makes it clear we're doing bitwise operations.
counter & (1 << j)
takes the result of that last operation, and does a bitwiseand
with the variablecounter
. It seems thatj
is a specifier for a bitmask - it tells which bit is important to check in incounter
. Since whatever(1 << j)
produces will only have one1
in its binary representation, the expressioncounter & (1 << j)
will always produce either a power of 2, or 0.
> 0
checks if the number that was produced was 0.
All in all, this is a fairly involved way to check if the j
th bit from the right in counter
is equal to 1
or 0
. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
add a comment |
Your statement:
if ( (counter & (1 << j)) > 0 ):
It's some bitwise operations. Let's break it down:
(1 << j)
generates the number0b1
and shifts it left byj
places -j
must be an integer. This is akin to saying2**j
, or 2 to thej
th power, but doing it with the bit-shifting operator<<
makes it clear we're doing bitwise operations.
counter & (1 << j)
takes the result of that last operation, and does a bitwiseand
with the variablecounter
. It seems thatj
is a specifier for a bitmask - it tells which bit is important to check in incounter
. Since whatever(1 << j)
produces will only have one1
in its binary representation, the expressioncounter & (1 << j)
will always produce either a power of 2, or 0.
> 0
checks if the number that was produced was 0.
All in all, this is a fairly involved way to check if the j
th bit from the right in counter
is equal to 1
or 0
. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.
Your statement:
if ( (counter & (1 << j)) > 0 ):
It's some bitwise operations. Let's break it down:
(1 << j)
generates the number0b1
and shifts it left byj
places -j
must be an integer. This is akin to saying2**j
, or 2 to thej
th power, but doing it with the bit-shifting operator<<
makes it clear we're doing bitwise operations.
counter & (1 << j)
takes the result of that last operation, and does a bitwiseand
with the variablecounter
. It seems thatj
is a specifier for a bitmask - it tells which bit is important to check in incounter
. Since whatever(1 << j)
produces will only have one1
in its binary representation, the expressioncounter & (1 << j)
will always produce either a power of 2, or 0.
> 0
checks if the number that was produced was 0.
All in all, this is a fairly involved way to check if the j
th bit from the right in counter
is equal to 1
or 0
. Without seeing the rest of your code, it's impossible to tell what the line does in context, but hopefully this gives you enough of a hint to whatever you're trying to figure out.
answered Nov 25 '18 at 5:52
Green Cloak GuyGreen Cloak Guy
3,4201721
3,4201721
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
add a comment |
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:31
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:32
add a comment |
The & operator is a bitwise operator. More information here.
Basically, if we consider counter as a binary:
counter = 0b0010
1 << j -> with j = 0
j will be the position that you want to evaluate with the 1.
Therefore, in this case the IF statement will not be executed because the AND operator will return 0.
But with:
counter = 0b0010
1 << j -> with j = 1
The IF statement will be executed because the AND operator will return 1.
To understand a little more, you can play with the following piece of code and change the values of counter and j:
counter = 0b0100
j = 2
if((counter & (1 << j)) > 0):
print("True")
else:
print("False")
add a comment |
The & operator is a bitwise operator. More information here.
Basically, if we consider counter as a binary:
counter = 0b0010
1 << j -> with j = 0
j will be the position that you want to evaluate with the 1.
Therefore, in this case the IF statement will not be executed because the AND operator will return 0.
But with:
counter = 0b0010
1 << j -> with j = 1
The IF statement will be executed because the AND operator will return 1.
To understand a little more, you can play with the following piece of code and change the values of counter and j:
counter = 0b0100
j = 2
if((counter & (1 << j)) > 0):
print("True")
else:
print("False")
add a comment |
The & operator is a bitwise operator. More information here.
Basically, if we consider counter as a binary:
counter = 0b0010
1 << j -> with j = 0
j will be the position that you want to evaluate with the 1.
Therefore, in this case the IF statement will not be executed because the AND operator will return 0.
But with:
counter = 0b0010
1 << j -> with j = 1
The IF statement will be executed because the AND operator will return 1.
To understand a little more, you can play with the following piece of code and change the values of counter and j:
counter = 0b0100
j = 2
if((counter & (1 << j)) > 0):
print("True")
else:
print("False")
The & operator is a bitwise operator. More information here.
Basically, if we consider counter as a binary:
counter = 0b0010
1 << j -> with j = 0
j will be the position that you want to evaluate with the 1.
Therefore, in this case the IF statement will not be executed because the AND operator will return 0.
But with:
counter = 0b0010
1 << j -> with j = 1
The IF statement will be executed because the AND operator will return 1.
To understand a little more, you can play with the following piece of code and change the values of counter and j:
counter = 0b0100
j = 2
if((counter & (1 << j)) > 0):
print("True")
else:
print("False")
answered Nov 25 '18 at 6:05
Manuel ReyesManuel Reyes
886
886
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464944%2fwhat-does-ifcounter-1-j-0-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do we know without knowing what is
counter
,i
andj
?<<
is bitwise left shift operator doc.– Austin
Nov 25 '18 at 5:43
It checks if bit number
j
incounter
is set.– Michael Butscher
Nov 25 '18 at 5:49
This was the program I was referrring to:geeksforgeeks.org/power-set
– I am L
Nov 25 '18 at 6:33