# Thread: Hi, I found this C code that...

1. ## Hi, I found this C code that...

...I don't quite understand. What does the (A >> 1) part do here and what would you do in C++ for the same effect?

Code:
#define Blend_SoftLight(A,B)    ((uint8)((B < 128) ? (2*((A >> 1)+64)) * (B/255):(255 - (2*(255-((A >> 1) + 64))*(255-B)/255))))
The above macro works with color values form 1 to 255. Myself I will be working with float values from 0.0 to 1.0.

2. right bit shift.

3. I suggest that you read the tutorial on bitwise operators.

4. Originally Posted by valaris
right bit shift.
Ok so lets say that A is a float value of 0.4, what should happen to that value in this case?

5. Ok so lets say that A is a float value of 0.4, what should happen to that value in this case?
You will probably get a compile error since the bit shift operators work with integral operands (unless they are overloaded, in which case what it does depends on what the author wrote).

6. Originally Posted by laserlight
You will probably get a compile error since the bit shift operators work with integral operands (unless they are overloaded, in which case what it does depends on what the author wrote it to do).
Yes sure but I need to convert this code to work with floats. So.. what's the way to do that? Take this small part as an example:

Code:
2*((A >> 1)+64)
The resulting float should be within 0.0 and 1.0 instead of between 1 and 255 which the code is working with. So I guess 64 stands for 255/4 here which in my case will be 1/4 = 0.25.

7. Yes sure but I need to convert this code to work with floats. So.. what's the way to do that?
Divide by two instead of right shifting by one.

8. Actually with the math that you have the equation works like this

A = 255
A /= 2;
2 * A
A + 64
A = 319

Nowhere close to what you've intended.

But there are at least 255 rational numbers between 0 and 1, so I believe the answer would be (64.0 + A) / 255.0

9. Originally Posted by laserlight
Divide by two instead of right shifting by one.
Ok great , that was what I was looking for. So what if it instead said (A << 8), would that be divide by 8 or 16 or...?

Thanks.

10. Originally Posted by citizen
Actually with the math that you have the equation works like this

A = 255
A /= 2;
2 * A
A + 64
A = 319

Nowhere close to what you've intended.

But there are at least 255 rational numbers between 0 and 1, so I believe the answer would be (64.0 + A) / 255.0
Yes but the later parts of the code make it between 1 and 255

My code would look like this

2.0f * ((A * 0.5f) + 0.25f)

11. Woops. Anyway, here's how you can figure out shifts in decimal.
If >>1 is the same as / 2, then >>2 is the same as / 4, >>4 is the same as / 8; see a pattern yet?

12. Originally Posted by citizen
Woops. Anyway, here's how you can figure out shifts in decimal.
If >>1 is the same as / 2, then >>2 is the same as / 4, >>4 is the same as / 8; see a pattern yet?
Ah yes I think I do. Thanks

13. And the other way round, <<, means multiplying with powers of 2.

14. Originally Posted by citizen
If >>1 is the same as / 2, then >>2 is the same as / 4, >>4 is the same as / 8; see a pattern yet?
WHOOPS typo! That pattern gives a *much* different result!