In C bits, multiply by 3 and divide by 16

后端 未结 5 620
孤独总比滥情好
孤独总比滥情好 2020-12-29 17:46

A buddy of mine had these puzzles and this is one that is eluding me. Here is the problem, you are given a number and you want to return that number times 3 and divided by 1

5条回答
  •  遥遥无期
    2020-12-29 17:51

    For this question you need to worry about the lost bits before your division (obviously). Essentially, if it is negative then you want to add 15 after you multiply by 3. A simple if statement (using your operators) should suffice.

    I am not going to give you the code but a step by step would look like,

    x = x*3
    

    get the sign and store it in variable foo.

    have another variable hold x + 15;

    Set up an if statement so that if x is negative it uses that added 15 and if not then it uses the regular number (times 3 which we did above).

    Then divide by 16 which you already showed you know how to do. Good luck!

提交回复
热议问题