1. ## Bitwise Questions

I'm trying to optimize my code and I have two bitwise questions:

I have a variable i. I am running a bitwise operation. If var1 = 0, then i = 1, if var1 = any other number, then i = 0. Would this work: i = ~(var1 & 0).

Heres my second operation:

I have a variable i. I am running a bitwise operation. If var1's 7th bit (its 0-7) = 1, then i = 128. if var1's 7th bit = 0 then i = 0.

I'm trying to avoid using if statements and am having issues with bitwise operations. Any help would be appreciated.

2. (var1 & 0) is always zero, so it won't do what you ask for.

Code:
`!var1`
will give the result you want. Of course, this is not a bitwise operator, but it's most likely possible to solve (in the compiled code) without branches, which I presume is your actual goal - whethter you use if-statements or not shouldn't be a goal in optimization, but avoiding branches/jumps certainly SHOULD be.

The second one can be solved with a simple and-statement - I'll let you post your attempt first - because that way, you will LEARN SOMETHING more than "I can post something and get an answer that I can paste into my code" - I think you know how to do the latter already.

--
Mats

3. Originally Posted by someprogr
i = var1 | 128 ?
Nope. Use AND, not OR.

--
Mats

4. Originally Posted by matsp
Nope. Use AND, not OR.

--
Mats
actually, wouldnt shifting it to the right and isolating it be faster? i.e. i = var1 >> 7

5. Originally Posted by someprogr
actually, wouldnt shifting it to the right and isolating it be faster?
Not if you want to have i = 128 when var1 has the bit no 7 set. If you want i = 1 when bit 7 is set, then shifting it would be the right thing to do. Shifting is never faster than and, but it is slower on at least older/smaller processors - modern high-end processors, and the result is probably the same amount of time to shift or mask the bit.

--
Mats

6. Originally Posted by matsp
Not if you want to have i = 128 when var1 has the bit no 7 set. If you want i = 1 when bit 7 is set, then shifting it would be the right thing to do. Shifting is never faster than and, but it is slower on at least older/smaller processors - modern high-end processors, and the result is probably the same amount of time to shift or mask the bit.

--
Mats
oh ok. thanks

7. bitwise AND'ing with octal 0100 ought to do the job as in
Code:
`i = (var1 & 0100) ? 128 : 0;`

8. Originally Posted by itCbitC
bitwise AND'ing with octal 0100 ought to do the job as in
Code:
`i = (var1 & 0100) ? 128 : 0;`
Ehm, yes, if bit 7 is the bit worth 64, then I would agree that this would achieve the desired effect - but the terniary operator is often implmented as a if-statement, so may lead to a branch, so if this is REALLY what you want to do, then:
Code:
`i = (var1 & 64) << 1;`
will achieve the same thing, guaranteed to be without a branch.

Using octal will achieve nothing special here.

--
Mats

9. Yes the ternary operator is a contraction of the if-else; the octal's only for the effect; and i = (var1 & 64) << 1; is a clever one