Hi, I found this C code that...

This is a discussion on Hi, I found this C code that... within the C++ Programming forums, part of the General Programming Boards category; ...I don't quite understand. What does the (A >> 1) part do here and what would you do in C++ ...

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

    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.
    home = hat.position;

  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
    21,633
    I suggest that you read the tutorial on bitwise operators.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    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?
    home = hat.position;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    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.
    home = hat.position;

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    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
    158
    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.
    home = hat.position;

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    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.
    home = hat.position;

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    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
    158
    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
    home = hat.position;

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    287
    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, 03: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-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21