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

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    166

    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. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    right bit shift.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read the tutorial on bitwise operators.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by valaris View Post
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by laserlight View Post
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by laserlight View Post
    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. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by citizen View Post
    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)
    Last edited by DrSnuggles; 10-11-2008 at 01:13 AM.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by citizen View Post
    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. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And the other way round, <<, means multiplying with powers of 2.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by citizen View Post
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM