Results 1 to 5 of 5

Thread: [RESOLVED] Bit Shifting with C

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Resolved [RESOLVED] Bit Shifting with C

    I've come to understand C coding quite a bit over the past week. I am not a C programmer at all. Trying to convert/interpret some C code, I've come across the following simple calculations and am unsure what the results would be. I do not own a C compiler.

    If anyone could provide the answers to the these 2 calculations and/or answer my following question, I would very much appreciate it.

    All variables are defined as: int

    Calc 1: x4 =-17728437 x5 =-3330316
    x2 = (181 * (x4 + x5) + 128) >> 8

    Calc 2: x4 = 33479030 x5 = 6609623
    x2 = (181 * (x4 + x5) + 128) >> 8

    Where I'm having problems with this is that calcs above, before shifting, will result in a value that needs more than 32 bits.
    Is there some sort of variable type promotion?
    or possibly truncation?

    When this is the case, what part of the result, exactly, whether positive or negative, is shifted? just 32 bits, 32+ bits, something else?
    Last edited by LaVolpe; Dec 20th, 2011 at 01:43 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Bit Shifting with C

    When the value exceeds the size of the 32 bit integer, it wraps around.
    To prove this, I did this:
    0xFFFFFFFF + 1
    Result = 0
    0xFFFFFFFF + 10
    Result = 9.

    The result (32 bit integer) is shifted 8 bytes to the right.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Bit Shifting with C

    Quote Originally Posted by Atheist View Post
    When the value exceeds the size of the 32 bit integer, it wraps around...

    The result (32 bit integer) is shifted 8 bytes to the right.
    In your examples:
    0xFFFFFFFF + 1 would be 0x100000000 (33 bits)
    0xFFFFFFFF + 10 would be 0x100000009 (33 bits)

    It appears that the right 32 bits (0xFFFFFFFF mask) are simply used and I expected that. In the calculations I provided in previous reply: the intermediary value, the stuff btwn the parentheses, is not directly assigned to a variable. And in this case, I don't know if C is shifting all bits or truncating it before shifting. Whether before or after, the result can be vastly different. If I had the actual result to the calculations, I could make that determination with confidence I think.

    Edited: Hmmm. From what I've read on variable declaration, when declaring a variable as INT, it is signed by default unless explicitly declaring it as unsigned int. If that's the case, then wouldn't 0xFFFFFFFF be -1? And if so, your addition examples make perfect sense too. If I've misunderstood that basic concept, I'll have to go back & relook at every line of code I've interpreted
    Last edited by LaVolpe; Dec 20th, 2011 at 03:24 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Bit Shifting with C

    Quote Originally Posted by LaVolpe View Post
    In your examples:
    0xFFFFFFFF + 1 would be 0x100000000 (33 bits)
    0xFFFFFFFF + 10 would be 0x100000009 (33 bits)
    Yes but not if we are dealing with 32-bit integers, which I assume is the case

    Quote Originally Posted by LaVolpe View Post
    It appears that the right 32 bits (0xFFFFFFFF mask) are simply used and I expected that. In the calculations I provided in previous reply: the intermediary value, the stuff btwn the parentheses, is not directly assigned to a variable. And in this case, I don't know if C is shifting all bits or truncating it before shifting. Whether before or after, the result can be vastly different. If I had the actual result to the calculations, I could make that determination with confidence I think.

    Edited: Hmmm. From what I've read on variable declaration, when declaring a variable as INT, it is signed by default unless explicitly declaring it as unsigned int. If that's the case, then wouldn't 0xFFFFFFFF be -1? And if so, your addition examples make perfect sense too. If I've misunderstood that basic concept, I'll have to go back & relook at every line of code I've interpreted
    msvc will treat integer constants as 32-bit values per default, unless explicitly defined otherwise by suppying a suffix.
    Take this for instance:
    Code:
    unsigned long long result64 = 4294967295 + 4294967295;
    The result will be 4294967295. Not quite what you'd expect if you thought you where dealing with 64-bit values.

    The outcome is different here:
    Code:
    unsigned long long result64 = 4294967295LL + 4294967295LL;
    The result is 8589934590.

    Although different compilers and/or platforms would behave differently.

    Yeah integers are per default signed.

    The result to the calculations in your original post is 1888020 and -5210502 respectively!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Bit Shifting with C

    Though the thread was educational, my problem was not with bit shifting after all. I misunderstood the precedence of ++ operator when on the left or right side of a pointer. Bottom line, I was using the wrong value for the x4, x5 variables.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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