-3

I'm working on a code in C that returns the index of the left most 1. It will also need to return -1 if there are no 1 in the binary number. My code resulted in error message from the terminal:

warning: control reaches end of non-void function [-Wreturn-type]

  • You are missing a right curly bracket there – asimes Sep 20 '14 at 21:31
  • 1
    That's not all your code - it's missing at least one right-brace. Which, since your error is about reaching the end of the function, is important. Also, think about what it means to have no bits in the number - what value must it have? – The Archetypal Paul Sep 20 '14 at 21:33
  • 1
    `return -1;` move to after for-loop. – BLUEPIXY Sep 20 '14 at 21:35
  • right brace is added, sorry i left it off. – RadScorpius Sep 20 '14 at 21:40
  • where did you define number_of_bits? Unless the compiler can tell it must be greater than zero, it must assume the for loop may not execute, and then you get to the end of the function without returning any value. Which explains the warning – The Archetypal Paul Sep 20 '14 at 21:42
  • @Paul, number_of_bits is a hardcoded number. It will remain unchanged in this program. – RadScorpius Sep 20 '14 at 21:43
  • How did you hard code it? You know it is not going to change, but the compiler may not – The Archetypal Paul Sep 20 '14 at 21:44
  • @Paul, all i did was: int i = 64; – RadScorpius Sep 20 '14 at 21:46
  • Other duplicates: http://stackoverflow.com/questions/671815/what-is-the-fastest-most-efficient-way-to-find-the-highest-set-bit-msb-in-an-i, http://stackoverflow.com/questions/2589096/find-most-significant-bit-left-most-that-is-set-in-a-bit-array?rq=1, http://stackoverflow.com/questions/671815/what-is-the-fastest-most-efficient-way-to-find-the-highest-set-bit-msb-in-an-i?lq=1, http://stackoverflow.com/questions/9041837/find-the-index-of-the-highest-bit-set-of-a-32-bit-number-without-loops-obviously?lq=1 – The Archetypal Paul Sep 20 '14 at 21:46
  • and the compiler may not be able to tell that nothing else in the program can change that (at least add a `const`!). Or as others have said, move the return -1 outside for loop – The Archetypal Paul Sep 20 '14 at 21:47

1 Answers1

0
int find_left_most_1 (int input) {
  if (input) {
    int i=number_of_bits;
    for (i=number_of_bits; i>0; i--){
      if (a_function(input,i)==1)
        return i;
    }
    return -1;
  }
  return -1;
}
ThisaruG
  • 3,222
  • 7
  • 38
  • 60