How to Find Most Significant Bit Position? [duplicate]
This question already has an answer here:
Find most significant set bit in a long
3 answers
I need to really find the most significant bit position of an unsigned n. It can be whatever input.
I don't want the bit value, rather I want the position of the most significant bit (it has to be 1 not 0 btw).
For example, if my computer reads 0011101110, the msb is 6.
Edit: I was using from an example my teacher gave. She may be wrong. I think the msb in that example is 7.
Also, I don't want a code written for me. Just explain what I should do. Like the concept of the thing. I don't wanna cheat or mooch off anyone.
New Edit: IDK how this is a duplicate as the one given to me that this is a duplicate of is nothing like my question. It wouldn't have answered a thing I asked here.
c position bit
marked as duplicate by Antti Haapala
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();
}
);
});
});
Nov 28 '18 at 7:45
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.
|
show 13 more comments
This question already has an answer here:
Find most significant set bit in a long
3 answers
I need to really find the most significant bit position of an unsigned n. It can be whatever input.
I don't want the bit value, rather I want the position of the most significant bit (it has to be 1 not 0 btw).
For example, if my computer reads 0011101110, the msb is 6.
Edit: I was using from an example my teacher gave. She may be wrong. I think the msb in that example is 7.
Also, I don't want a code written for me. Just explain what I should do. Like the concept of the thing. I don't wanna cheat or mooch off anyone.
New Edit: IDK how this is a duplicate as the one given to me that this is a duplicate of is nothing like my question. It wouldn't have answered a thing I asked here.
c position bit
marked as duplicate by Antti Haapala
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();
}
);
});
});
Nov 28 '18 at 7:45
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.
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
1
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
2
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45
|
show 13 more comments
This question already has an answer here:
Find most significant set bit in a long
3 answers
I need to really find the most significant bit position of an unsigned n. It can be whatever input.
I don't want the bit value, rather I want the position of the most significant bit (it has to be 1 not 0 btw).
For example, if my computer reads 0011101110, the msb is 6.
Edit: I was using from an example my teacher gave. She may be wrong. I think the msb in that example is 7.
Also, I don't want a code written for me. Just explain what I should do. Like the concept of the thing. I don't wanna cheat or mooch off anyone.
New Edit: IDK how this is a duplicate as the one given to me that this is a duplicate of is nothing like my question. It wouldn't have answered a thing I asked here.
c position bit
This question already has an answer here:
Find most significant set bit in a long
3 answers
I need to really find the most significant bit position of an unsigned n. It can be whatever input.
I don't want the bit value, rather I want the position of the most significant bit (it has to be 1 not 0 btw).
For example, if my computer reads 0011101110, the msb is 6.
Edit: I was using from an example my teacher gave. She may be wrong. I think the msb in that example is 7.
Also, I don't want a code written for me. Just explain what I should do. Like the concept of the thing. I don't wanna cheat or mooch off anyone.
New Edit: IDK how this is a duplicate as the one given to me that this is a duplicate of is nothing like my question. It wouldn't have answered a thing I asked here.
This question already has an answer here:
Find most significant set bit in a long
3 answers
c position bit
c position bit
edited Nov 28 '18 at 7:50
May Athena
asked Nov 28 '18 at 6:29
May AthenaMay Athena
65
65
marked as duplicate by Antti Haapala
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();
}
);
});
});
Nov 28 '18 at 7:45
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 Antti Haapala
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();
}
);
});
});
Nov 28 '18 at 7:45
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.
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
1
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
2
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45
|
show 13 more comments
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
1
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
2
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
1
1
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
2
2
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45
|
show 13 more comments
1 Answer
1
active
oldest
votes
Ok, you need 4 things:
1) The sizeof operator, which gives you your variables size in bytes. To get the number of bits, you simply multiply with the constant CHAR_BIT. You get that constant by including 'limits.h'.
2) A loop where you run through all the bits in your variable, using a separate counter variable. Inside the loop, you use
3) the binary 'and' & to test your variable against the value of 1, which you keep increasing by powers of 2 inside your loop using the
4) binary left shift operator <<
Inside the loop you check if that binary 'and' results in '1'; if yes the bit is set and you can increment set another variable to your loop counter, which will be your return value.
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok, you need 4 things:
1) The sizeof operator, which gives you your variables size in bytes. To get the number of bits, you simply multiply with the constant CHAR_BIT. You get that constant by including 'limits.h'.
2) A loop where you run through all the bits in your variable, using a separate counter variable. Inside the loop, you use
3) the binary 'and' & to test your variable against the value of 1, which you keep increasing by powers of 2 inside your loop using the
4) binary left shift operator <<
Inside the loop you check if that binary 'and' results in '1'; if yes the bit is set and you can increment set another variable to your loop counter, which will be your return value.
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
add a comment |
Ok, you need 4 things:
1) The sizeof operator, which gives you your variables size in bytes. To get the number of bits, you simply multiply with the constant CHAR_BIT. You get that constant by including 'limits.h'.
2) A loop where you run through all the bits in your variable, using a separate counter variable. Inside the loop, you use
3) the binary 'and' & to test your variable against the value of 1, which you keep increasing by powers of 2 inside your loop using the
4) binary left shift operator <<
Inside the loop you check if that binary 'and' results in '1'; if yes the bit is set and you can increment set another variable to your loop counter, which will be your return value.
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
add a comment |
Ok, you need 4 things:
1) The sizeof operator, which gives you your variables size in bytes. To get the number of bits, you simply multiply with the constant CHAR_BIT. You get that constant by including 'limits.h'.
2) A loop where you run through all the bits in your variable, using a separate counter variable. Inside the loop, you use
3) the binary 'and' & to test your variable against the value of 1, which you keep increasing by powers of 2 inside your loop using the
4) binary left shift operator <<
Inside the loop you check if that binary 'and' results in '1'; if yes the bit is set and you can increment set another variable to your loop counter, which will be your return value.
Ok, you need 4 things:
1) The sizeof operator, which gives you your variables size in bytes. To get the number of bits, you simply multiply with the constant CHAR_BIT. You get that constant by including 'limits.h'.
2) A loop where you run through all the bits in your variable, using a separate counter variable. Inside the loop, you use
3) the binary 'and' & to test your variable against the value of 1, which you keep increasing by powers of 2 inside your loop using the
4) binary left shift operator <<
Inside the loop you check if that binary 'and' results in '1'; if yes the bit is set and you can increment set another variable to your loop counter, which will be your return value.
edited Nov 28 '18 at 7:59
answered Nov 28 '18 at 7:34
GermanNerdGermanNerd
512111
512111
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
add a comment |
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
I am so sorry I cannot upvote your comment. It wont show bc I am new here so my vote doesnt show. But your explanation really helps. Thank you! That's what I wanted. Understanding the concept will let me figure out how to do this to other questions, not just this one, ya know? Thank you!
– May Athena
Nov 28 '18 at 7:37
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
You are welcome. It can be frustrating if you get so little instructions from your teacher, and I guess English is not your 1st language, either...If my answer is of use to you, you can accept it, even with low reputation.
– GermanNerd
Nov 28 '18 at 7:38
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
No, English is my first language. Programming isn't though lol.
– May Athena
Nov 28 '18 at 7:49
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
Ups...sorry. I'm not so used to the way young people write English. ;) BTW, if you have trouble understanding what these binary operations really do (& and <<), I can edit my answer to show a little diagram.
– GermanNerd
Nov 28 '18 at 7:53
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
No. It's fine. This helped enough for me! :)
– May Athena
Nov 28 '18 at 9:43
add a comment |
Please show us what you have tried so far!! Noboby wants to write your software.
– Mike
Nov 28 '18 at 6:30
1
Your example does not make sense, at least to me.
– Osiris
Nov 28 '18 at 6:32
2
"... 0011101110, the msb is 6" How?
– 4386427
Nov 28 '18 at 6:34
I dont want anyone to write my code. I just want to know what to do to find the msb position. Just an explanation. I don't want a code written for me.
– May Athena
Nov 28 '18 at 6:44
It was the example our teacher gave. Word for word its "For example: 0011101110. The msb is 6. Use this as a way to test." Personally I think it's 7. Counting right to left, starting at 0, it might be 7 actually.
– May Athena
Nov 28 '18 at 6:45