1. ## Dealing with bits

i did my best to do this function, the following is what i came up with.
However, the max operators that i can use is 12 and i'm using 16. i did all i

/ * multFiveEight - multiplies by 5/8 rounding toward 0.
* Examples: multFiveEights(77) = 48
* multFiveEights(-22) = -13
* You can assume |x| < (1 << 29)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
Code:
```int multFiveEight(int x) {
int ones=~0;
int y=(x<<2)+x;
int y_neg=(~(y>>4))+1;
int y_pos=y>>3;
return ((ones+pos)&y_neg)|((ones+!pos)&y_pos);
}```

2. i edited it.

3. Dividing a number by eight and bitshifting that number right by 3 is the same thing. That should help you cut down on the number of operations.

4. Can you clarify that more! what piece of code i should remove.

5. I always hate these type of assignments because I place very little value on
them - I highly doubt this appears in any real code, and for teaching purposes,
either your brain is wired to do this type of thing in your sleep, or it isn't.

Anyhoo, optimisations:

Code:
```int negMask=1<<31;
.
.
.
int ones=~0;```
why exactly do you need operators to determine these constants? bit shifts do
integer multiplication/division for you - if you shift a number left by n, you multiply
it by a factor of 2^n (naturally assuming that no overflow occurs). So you can
replace the first operation by just assigning this variable with the value of 2^31,
and the ~ operator just inverts all the bits, so you can determine that value
also.

There's probably more you can do, but I won't try to go into more, but if you
rethink your approach, you can do this in 3 operations!

EDIT: Didn't think about signed numbers.

6. Yes, I believe that's all there in these two lines.
Code:
`  int y=(x<<2)+x;`
Code:
`  int y_pos=y>>3;`
There's the multiply by 5 and the divide by 8.

The hard part is what else the assignment requires.

My pet peeve it that when dealing with bits, signed values are used.

7. Originally Posted by Dave_Sinkula
My pet peeve it that when dealing with bits, signed values are used.
It shouldn't really matter, that's part of the joy of two's complement form for negative numbers. You should be able to use the bitwise math just the same.

8. So what happens when the target architecture doesn't use two's complement for negative numbers?

9. Originally Posted by zx-1
So what happens when the target architecture doesn't use two's complement for negative numbers?
Is there a single platform made in the past 30 years* that doesn't?

* Except machines made to operate on BCD

10. Originally Posted by Cat
Is there a single platform made in the past 30 years* that doesn't?

* Except machines made to operate on BCD
Why does everyone ask this question backwards?

If there ever was, is now, or ever will be why should I write my code so it breaks there rather than write it correctly?

11. Originally Posted by Dave_Sinkula
Why does everyone ask this question backwards?

If there ever was, is now, or ever will be why should I write my code so it breaks there rather than write it correctly?
Because the OP is learning how bitwise operations work, and bitwise operations on numbers require assumptions about the underlying representation of the numbers.

I mean, hell, if it wasn't an exercise to learn about bitwise operations, the OP could just do y = 5 * x / 8; and be done with it.

12. Originally Posted by Cat
Because the OP is learning how bitwise operations work, and bitwise operations on numbers require assumptions about the underlying representation of the numbers.
Or not, which is my point.

Or rather, they should learn to use unsigned values with bits. Otherwise it's like learning how to ........ in the wind.

13. I don't believe there is ANY bitwise operation that can be guaranteed to work without assuming the underlying numeric representation. I mean, it would be theoretically legal for a platform to define, for example, "int" as a series of 8 BCD digits. Then, for example x << 2 and x * 4 are completely different results.

14. Originally Posted by Cat
I don't believe there is ANY bitwise operation that can be guaranteed to work without assuming the underlying numeric representation.
Given the constraints of the language, this is demonstrably false with unsigned values[/edit].

15. There are systems that use binary coded decimal for numbers... for example, the number 128 represented as 0001 0010 1000 in binary. In such a case 128 << 1 should yield 0010 0101 0000 (250) not 256. I know IBM mainframes once used BCD, I believe they still use some variant.