So the number that gets returned is going to be the largest power of 2 that is less than or equal to the number that is passed by argument. My thinking is bitshift right until you have only 0001, then bitshift back left the same number of times and you have the answer. You could have a while loop that repeated bitshifts right and increments a tracking variable by one, and the loop terminates when the number is == 1. Then you run through a for loop bitshifting left that number of times. What's getting me is the rules of this task. No loops, no variables, no conditionals. I can't do any of that. It seems like the only thing that is within the rules is writing a single return statement that processes the whole thing at once?
This seems extraordinarily difficult. Solve this with one statement? Seems impossible. Unless there's something obvious I'm missing. Which I must be since others have completed this. Any hints? I'm completely stumped.
Having a hard time figuring out where to start
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
23 July 2022, 06:36
This is one of the tasks you can not solve without reading more about the current topic.
You can use several statements but not define new variables or use loops and conditions. So the only way you have is to use the number parameter and to manipulate that till you have your result.
Your start is not bad but what you need is a bit mask that keeps the highest bit (cause you can not create new vars and save the current state). So try to fill up all bits from the highest one to the right (shift number and or connect with number). So a 0010 1011 will become a 0011 1111. That's several steps.
The next part is fun. You take that new bitmask and shift it once to the right 0011 1111 becomes 0001 1111, negate it (1110 0000). Now you see the mask created in the former steps (0011 1111) and the new one have both the highest bit set but no other bits in common. So you AND connect them and return.
+1
Justin Smith
23 July 2022, 17:36
Ah, interesting. For some reason it didn't occur to me that I was allowed to change the value of the argument passed. I walled myself into thinking I basically couldn't do anything except a return statement.
0
Justin Smith
23 July 2022, 18:36
I seem to be having an issue with the ~ operator. According to what I've read online, it's supposed to flip all the 1s and 0s. So for example, if I have the number 5 (binary 0000 0101), using ~5 should give me a binary number of 1111 1010, right? But instead it gives me a binary number output of -110.
0
Thomas
23 July 2022, 19:00
huh... it should work as expected and ~5 should result in decimal -6 ((5 + 1) * -1). That's cause int is signed and if you flip all bytes to first byte (indicating the sign) will get flipped, too and so make the number negative (if it was positive before)
I tried and it worked smooth as butter for me
0
Justin Smith
23 July 2022, 19:22
This is what I get for a result when I test the given code with a value of 5.
0
Justin Smith
23 July 2022, 20:11
Since I now have working code that I want to post, I am going to make a new thread and mark this one resolved.
0
Thomas
23 July 2022, 20:17
don't use toString to verify... use toBinaryString or output in dec (as written above in dec it has to be (num + 1) * -1) -> 5 negated is -6, 11 negated is -12
what to String does: it takes a decimal (here ~5, that's dec -6) and strips of the most sigificant bit (the first bit) and writes a - instead (if the number is negative). Then just 6 decimal is left and that's 110, so the result is -110. That's not what you expected but how toString works
0