Results 1 to 8 of 8

Thread: bit shifting question

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    bit shifting question

    I'm still not a bit shifting pro so I gotta ask.

    DWORD 0xFF000000

    FF = Alpha Value = 255

    How can I directly modify that byte?
    Do I bitshift << 24 then set it with >> 24?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: bit shifting question

    Code:
    DWORD packed;
    packed = alpha << 24;
    alpha = packed >> 24;
    Or better:
    Code:
    #define LITTLE_ENDIAN // x86 = little endian
    #ifdef LITTLE_ENDIAN
       union Color {
           DWORD dword;
           unsigned char red,green,blue,alpha;
       };
    #else
       union Color {
           DWORD dword;
           unsigned char alpha,blue,green,red;
       };
    #endif
    Windows probably has some functions that do something like this.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: bit shifting question

    HIWORD and LOWORD, and HIBYTE and LOBYTE, but they're not endian-aware.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: bit shifting question

    I know this is a bump but I just cannot get it right...

    DWORD dwAlpha = pVertices->Buffer()[i].diffuse;
    dwAlpha = ((DWORD)(fAlpha * 200)) << iModifyDiffuseBit;
    pVertices->Buffer()[i].diffuse = dwAlpha >> iModifyDiffuseBit;

    The first line stores the DWORD diffuse into the dwAlpha.
    The next calculates a new alpha, or depending on iModifyDiffuseBit the RGB value's.
    The last line is suppose to replace the same bit on the diffuse now.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: bit shifting question

    Given that the second line overrides the value assigned in the first, the first is useless.
    Also, the shift in the second line and the one in the third line directly counteract each other, thus the effect is the same as if neither was there.

    What is the result you expect, what do you actually get?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: bit shifting question

    LoL, I cannot believe such basic mistakes.

    I need to take the diffuse, modify a certain bit of it and replace the diffuse with the modifed one.

    Example:
    0xFFA0A0A0;
    Modify and return
    0xBBA0A0A0;
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: bit shifting question

    Ah.

    OK, assuming you want the float alpha value fAlpha to replace the 0-255 alpha value in the highest byte of diffuse, you'd do:
    Code:
    DWORD dwAlpha = static_cast<DWORD>(fAlpha * 255);
    bla.diffuse = (0x00FFFFFF & bla.diffuse) | (dwAlpha << 24);
    The first line just scales the value up. (Note that this is not the best way, as it tends to undervalue just a bit.)
    The first part of the second line blanks out the alpha value of the combined colour, the second part puts the new alpha at the right position and mixes it in.
    If you want a different colour channel replaced, you must change mask and offset.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: bit shifting question

    Changing offset is easy, how would one simply change the mask?
    I do need to be able to pass in a diffuse and modify different values.

    I know, it would be easy, write a large if then to determine the necessary mask.
    Is there not a better way?


    Edit:



    Sometimes.... I'll just pass in the mask...
    Last edited by Halsafar; Jul 11th, 2005 at 07:52 PM.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width