I am working on a project my Professor has given to the class that requires us to count the number of bits a base 2 number has after being converted from base 10. We are not allowed to use any loops and not allowed to introduce any conditional statements, which means that we are limited to changing the return value to a ternary operator instead of having if/then statements. This is also supposed to contain a recursive call as well because as I stated previously we aren't allowed to have loops.
As I said before this is a homework assignment, and I would prefer to not post my entire code because I really don't want to just be given the answer. I would like to learn the language as much as possible. I also want to apologize if this question has already been presented, but I did a search before posting as the rules state, and came up empty handed.
Here is the ternary operator that is giving me an error when trying to compile. Also the error says "2 cannot be used as a function"
Because you're thinking in terms of algebraic evaluation. What we write on paper as 2(bitcounter(x-1) evaluates to 2 multiplied by whatever recursive value you get from bitcounter().
However, what the compiler interprets that is, anything with a parentheses, such that (), is interpreted as function().
Ahh I see, I was able to play around with it and actually get it to compile but I am having trouble getting it to add the bits of a binary number. Now I am getting a different error saying "bitCounter(x & 1u) cannot be written as a function"
So to address this piece of code: return (x&2!=0) ? (bitCounter(x%2)(bitCounter(x-1)))
Ternary operators don't work like that. Syntax is: condition ? (if_true) : (if_false)
Notice how you didn't have that in your previous post. For the compiler, it doesn't matter if your Booleans are ass-miles long, as long as those conditions are present, being that you need a comparison condition, if_true, if_false, and most importantly, these little suckers ?: