if (counter & (1<<j)) .what does this statement mean and how it works? [duplicate]What are bitwise shift (bit-shift) operators and how do they work?What does Str1[j] = (portData >> j) & 0x1; mean in C#What does 0 mean when initializing an object?What does “static” mean in C?How do function pointers in C work?What does the question mark and the colon (?: ternary operator) mean in objective-c?What does “dereferencing” a pointer mean?What does “CONST func(arg);” mean in C language?What does the C ??!??! operator do?Why does ENOENT mean “No such file or directory”?What does (x ^ 0x1) != 0 mean?LNK2091 error for OCIObjectGetAttr and OCIObjectSetAttr
Arthur Somervell: 1000 Exercises - Meaning of this notation
Theorems that impeded progress
The use of multiple foreign keys on same column in SQL Server
can i play a electric guitar through a bass amp?
Why do falling prices hurt debtors?
How to say job offer in Mandarin/Cantonese?
What does "Puller Prush Person" mean?
How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?
Which models of the Boeing 737 are still in production?
LaTeX closing $ signs makes cursor jump
Why, historically, did Gödel think CH was false?
How does strength of boric acid solution increase in presence of salicylic acid?
Languages that we cannot (dis)prove to be Context-Free
What defenses are there against being summoned by the Gate spell?
Can I ask the recruiters in my resume to put the reason why I am rejected?
Is it unprofessional to ask if a job posting on GlassDoor is real?
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
What's the point of deactivating Num Lock on login screens?
Can I make popcorn with any corn?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
How does one intimidate enemies without having the capacity for violence?
How much RAM could one put in a typical 80386 setup?
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
if (counter & (1
What are bitwise shift (bit-shift) operators and how do they work?What does Str1[j] = (portData >> j) & 0x1; mean in C#What does 0 mean when initializing an object?What does “static” mean in C?How do function pointers in C work?What does the question mark and the colon (?: ternary operator) mean in objective-c?What does “dereferencing” a pointer mean?What does “CONST func(arg);” mean in C language?What does the C ??!??! operator do?Why does ENOENT mean “No such file or directory”?What does (x ^ 0x1) != 0 mean?LNK2091 error for OCIObjectGetAttr and OCIObjectSetAttr
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
What are bitwise shift (bit-shift) operators and how do they work?
8 answers
i was working on an algorithm of sub sequences.
please tell me the meaning of this statement.
program is as below.
void printSubsequences(int arr[], int n)
unsigned int opsize = pow(2, n);
for (int counter = 1; counter < opsize; counter++)
for (int j = 0; j < n; j++)
if (counter & (1<<j))
cout << arr[j] << " ";
cout << endl;
c
marked as duplicate by Mitch Wheat, Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 13 '17 at 9:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What are bitwise shift (bit-shift) operators and how do they work?
8 answers
i was working on an algorithm of sub sequences.
please tell me the meaning of this statement.
program is as below.
void printSubsequences(int arr[], int n)
unsigned int opsize = pow(2, n);
for (int counter = 1; counter < opsize; counter++)
for (int j = 0; j < n; j++)
if (counter & (1<<j))
cout << arr[j] << " ";
cout << endl;
c
marked as duplicate by Mitch Wheat, Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 13 '17 at 9:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g.1 << 3
means1*2*2*2
.
– M.M
Jan 13 '17 at 7:42
2
Thecounter & (stuff)
results in a bitwiseAND
ofcounter
andstuff
– David C. Rankin
Jan 13 '17 at 7:45
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47
add a comment |
This question already has an answer here:
What are bitwise shift (bit-shift) operators and how do they work?
8 answers
i was working on an algorithm of sub sequences.
please tell me the meaning of this statement.
program is as below.
void printSubsequences(int arr[], int n)
unsigned int opsize = pow(2, n);
for (int counter = 1; counter < opsize; counter++)
for (int j = 0; j < n; j++)
if (counter & (1<<j))
cout << arr[j] << " ";
cout << endl;
c
This question already has an answer here:
What are bitwise shift (bit-shift) operators and how do they work?
8 answers
i was working on an algorithm of sub sequences.
please tell me the meaning of this statement.
program is as below.
void printSubsequences(int arr[], int n)
unsigned int opsize = pow(2, n);
for (int counter = 1; counter < opsize; counter++)
for (int j = 0; j < n; j++)
if (counter & (1<<j))
cout << arr[j] << " ";
cout << endl;
This question already has an answer here:
What are bitwise shift (bit-shift) operators and how do they work?
8 answers
c
c
edited Jan 13 '17 at 7:43
Codor
15.7k82648
15.7k82648
asked Jan 13 '17 at 7:40
Sarathi ShahSarathi Shah
3215
3215
marked as duplicate by Mitch Wheat, Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 13 '17 at 9:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mitch Wheat, Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 13 '17 at 9:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g.1 << 3
means1*2*2*2
.
– M.M
Jan 13 '17 at 7:42
2
Thecounter & (stuff)
results in a bitwiseAND
ofcounter
andstuff
– David C. Rankin
Jan 13 '17 at 7:45
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47
add a comment |
2
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g.1 << 3
means1*2*2*2
.
– M.M
Jan 13 '17 at 7:42
2
Thecounter & (stuff)
results in a bitwiseAND
ofcounter
andstuff
– David C. Rankin
Jan 13 '17 at 7:45
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47
2
2
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g. 1 << 3
means 1*2*2*2
.– M.M
Jan 13 '17 at 7:42
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g. 1 << 3
means 1*2*2*2
.– M.M
Jan 13 '17 at 7:42
2
2
The
counter & (stuff)
results in a bitwise AND
of counter
and stuff
– David C. Rankin
Jan 13 '17 at 7:45
The
counter & (stuff)
results in a bitwise AND
of counter
and stuff
– David C. Rankin
Jan 13 '17 at 7:45
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47
add a comment |
1 Answer
1
active
oldest
votes
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
add a comment |
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
add a comment |
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
edited Jun 20 '18 at 4:32
Azeem
3,10941224
3,10941224
answered Jan 13 '17 at 7:46
CodorCodor
15.7k82648
15.7k82648
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
add a comment |
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
1
1
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
can you please give me step by step execution of it?
– Sarathi Shah
Jan 13 '17 at 8:36
1
1
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
The first loop generates all possible combinations and the second loop print according to those combinations. @SarathiShah
– shinzou
Mar 2 '18 at 21:50
add a comment |
2
<<
means that the left-hand operand is multiplied by 2, for as many times as the number in the right-hand operand. E.g.1 << 3
means1*2*2*2
.– M.M
Jan 13 '17 at 7:42
2
The
counter & (stuff)
results in a bitwiseAND
ofcounter
andstuff
– David C. Rankin
Jan 13 '17 at 7:45
What are bitwise shift (bit-shift) operators and how do they work?
– phuclv
Jan 13 '17 at 7:47