-2

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.

May Athena
  • 39
  • 8
  • Please show us what you have tried so far!! Noboby wants to write your software. – Mike Nov 28 '18 at 06:30
  • 1
    Your example does not make sense, at least to me. – Osiris Nov 28 '18 at 06:32
  • 2
    "... 0011101110, the msb is 6" How? – Support Ukraine Nov 28 '18 at 06: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 06: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 06:45
  • @MayAthena 7 is correct. Anyway - do you have any code to share so far? Any ideas of the operators to use? SO don't just write code for you. You need to show some effort by sharing the code you have or at least a description of your ideas. – Support Ukraine Nov 28 '18 at 06:49
  • @MayAthena Here is a tip to get you started. To check if the bit at position 3 is set you can do: `if (n & (1 << 3)) printf("The bit at position 3 is set\n");` Now take this code and use a variable instead of the hard coded 3. Then put it into a for-loop. Then you are nearly done. – Support Ukraine Nov 28 '18 at 06:52
  • @ 4386427 Good idea! I will try. – May Athena Nov 28 '18 at 06:58
  • I dont have a code at all is the problem. I had to scratch my code ten times already. I know how to find lsb, but msb....I am lost. – May Athena Nov 28 '18 at 06:59
  • @MayAthena lsb or msb.. same thing... For msb you just start at `pos = CHAR_BIT * sizeof(unsigned) - 1` and **decrement** `pos`in each loop until you find a bit that is set. (include limits.h to get CHAR_BIT) – Support Ukraine Nov 28 '18 at 07:01
  • Write a loop. You can either test different bit positions, as suggested by @4386427. Or you can bit-shift (half the value) of the number to test until it becomes 0. – Roland Weber Nov 28 '18 at 07:02
  • I dont know what any of you just said. I am noob noob. How does one go about bit-shifting the number and what does it do to help find the most significant bit? – May Athena Nov 28 '18 at 07:04
  • I sound annoying. for sure. I know already. But pls explain like I am a child. Our teacher only has had 30 minutes per class to teach this, so I am running on virtually no information. – May Athena Nov 28 '18 at 07:06
  • Thank you, but this doesnt help me. It just says the bit is set. What does that mean? I need a position listed out. Like in my example. I just want some example or explanation of how this works, not an answer. I dont want for anyone to write my code (hence why I am not asking for a code, just an explanation). – May Athena Nov 28 '18 at 07:14
  • Oh, wait so set means it's a 1. Like, it has a set value? Bc 0 means, well, 0. I am just trying to understand the terminology. :) – May Athena Nov 28 '18 at 07:25
  • Okay, thank you so much! As for my code...My code would look pretty empty as I only have my function prototype and standard main. No point putting this code out. :( I didn't know what to do to start bc I didn't know what the teacher asked of us. We never covered bit manipulation for more than ten minutes. So making loops etc, kinda requires an understanding of how the manipulation affects the bits. – May Athena Nov 28 '18 at 07:27
  • @May Athena: The duplicate link given above solves your question **exactly**, and in a very elegant way. However, I think that a beginner in C will have great difficulties in understanding that code. – GermanNerd Nov 28 '18 at 08:31
  • Boo. Well that sucks. Guess this site doesnt recognize skill level lol. Though it figures bc everyone here is a genius. :( – May Athena Nov 28 '18 at 09:42

1 Answers1

0

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
GermanNerd
  • 643
  • 5
  • 12
  • 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 07: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 07:38
  • No, English is my first language. Programming isn't though lol. – May Athena Nov 28 '18 at 07: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 07:53
  • No. It's fine. This helped enough for me! :) – May Athena Nov 28 '18 at 09:43