Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. When 1 != 1

When 1 != 1

Scheduled Pinned Locked Moved Clever Code
comtoolsquestion
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peterchen
    wrote on last edited by
    #1

    Here's the code:

    enum EAxisType
    {
    AxisX = 0,
    AxisY,
    AxisZ
    };

    struct AxisID
    {
    EAxisType axisType : 2; // X Y or Z
    int axisIdx : 8; // multiple axes allowed
    // ...
    };

    vod Surprise()
    {
    AxisID axisID;
    axisID.axisType = AxisZ;
    assert(axisID.axisType == AxisZ); // fails! (VC6, VC8)
    }

    Why? :cool: It took me a while to figure out, and I am not 100% sure what the standard says about this. Might not be reproducable on your compiler, or depend on a compiler option. axisIdx doesn't matter and can be omitted


    Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
    Linkify!|Fold With Us!

    W R 2 Replies Last reply
    0
    • P peterchen

      Here's the code:

      enum EAxisType
      {
      AxisX = 0,
      AxisY,
      AxisZ
      };

      struct AxisID
      {
      EAxisType axisType : 2; // X Y or Z
      int axisIdx : 8; // multiple axes allowed
      // ...
      };

      vod Surprise()
      {
      AxisID axisID;
      axisID.axisType = AxisZ;
      assert(axisID.axisType == AxisZ); // fails! (VC6, VC8)
      }

      Why? :cool: It took me a while to figure out, and I am not 100% sure what the standard says about this. Might not be reproducable on your compiler, or depend on a compiler option. axisIdx doesn't matter and can be omitted


      Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
      We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
      Linkify!|Fold With Us!

      W Offline
      W Offline
      Warren Stevens
      wrote on last edited by
      #2

      because the default type for the enum is signed, so one of the two bits is used for a sign, and AxisZ (which is 2) overflows the range (which is zero or one). Could be fixed by changing the 2 to a 3, or adding ": unsigned" to the end of the enum declaration (to specify the type of the enum). Out of curiosity, I'd like to know why you are using bit fields anyway. How little RAM does your target machine have, or how big is your data set?


      Want robust software? Use the new Vista Kernel Transaction Manager[^]


      www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

      P D 2 Replies Last reply
      0
      • W Warren Stevens

        because the default type for the enum is signed, so one of the two bits is used for a sign, and AxisZ (which is 2) overflows the range (which is zero or one). Could be fixed by changing the 2 to a 3, or adding ": unsigned" to the end of the enum declaration (to specify the type of the enum). Out of curiosity, I'd like to know why you are using bit fields anyway. How little RAM does your target machine have, or how big is your data set?


        Want robust software? Use the new Vista Kernel Transaction Manager[^]


        www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

        P Offline
        P Offline
        peterchen
        wrote on last edited by
        #3

        Right - I had to debug the assembly to see it :doh: For Hysterical Raisins - AxisIDs are passed around as DWORDs, and used as array indices in some places, and I am not going to mess around with this (although it looks as if I could).


        Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
        We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
        Linkify!|Fold With Us!

        W 1 Reply Last reply
        0
        • P peterchen

          Right - I had to debug the assembly to see it :doh: For Hysterical Raisins - AxisIDs are passed around as DWORDs, and used as array indices in some places, and I am not going to mess around with this (although it looks as if I could).


          Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
          Linkify!|Fold With Us!

          W Offline
          W Offline
          Warren Stevens
          wrote on last edited by
          #4

          peterchen wrote:

          Right - I had to debug the assembly to see it

          I admit I had to use the debugger to see the change, but once I saw it go to a negative number, I immediately suspected a signed/unsigned issue. Warren P.S. Great Addin article: http://www.codeproject.com/tools/vs2005addinmgr.asp[^] I had a trial version of an Add-in that expired (which wouldn't let me remove it easily) :-D


          Want robust software? Use the new Vista Kernel Transaction Manager[^]


          www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

          P 1 Reply Last reply
          0
          • W Warren Stevens

            because the default type for the enum is signed, so one of the two bits is used for a sign, and AxisZ (which is 2) overflows the range (which is zero or one). Could be fixed by changing the 2 to a 3, or adding ": unsigned" to the end of the enum declaration (to specify the type of the enum). Out of curiosity, I'd like to know why you are using bit fields anyway. How little RAM does your target machine have, or how big is your data set?


            Want robust software? Use the new Vista Kernel Transaction Manager[^]


            www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

            D Offline
            D Offline
            dighn
            wrote on last edited by
            #5

            I had almost forgotten about bit fields until this thread reminded me of it.

            1 Reply Last reply
            0
            • W Warren Stevens

              peterchen wrote:

              Right - I had to debug the assembly to see it

              I admit I had to use the debugger to see the change, but once I saw it go to a negative number, I immediately suspected a signed/unsigned issue. Warren P.S. Great Addin article: http://www.codeproject.com/tools/vs2005addinmgr.asp[^] I had a trial version of an Add-in that expired (which wouldn't let me remove it easily) :-D


              Want robust software? Use the new Vista Kernel Transaction Manager[^]


              www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #6

              Warren Stevens wrote:

              Great Addin article: http://www.codeproject.com/tools/vs2005addinmgr.asp\[^\] I had a trial version of an Add-in that expired (which wouldn't let me remove it easily)

              Thanks! (It helps to know it's used :))


              Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
              Linkify!|Fold With Us!

              1 Reply Last reply
              0
              • P peterchen

                Here's the code:

                enum EAxisType
                {
                AxisX = 0,
                AxisY,
                AxisZ
                };

                struct AxisID
                {
                EAxisType axisType : 2; // X Y or Z
                int axisIdx : 8; // multiple axes allowed
                // ...
                };

                vod Surprise()
                {
                AxisID axisID;
                axisID.axisType = AxisZ;
                assert(axisID.axisType == AxisZ); // fails! (VC6, VC8)
                }

                Why? :cool: It took me a while to figure out, and I am not 100% sure what the standard says about this. Might not be reproducable on your compiler, or depend on a compiler option. axisIdx doesn't matter and can be omitted


                Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                Linkify!|Fold With Us!

                R Offline
                R Offline
                Ri Qen Sin
                wrote on last edited by
                #7

                One more note: This vod Surprise() { AxisID axisID; axisID.axisType = AxisZ; assert(axisID.axisType == AxisZ); // fails! (VC6, VC8) } becomes void Surprise() { AxisID axisID; axisID.axisType = AxisZ; assert(axisID.axisType == AxisZ); // fails! (VC6, VC8) }

                ROFLOLMFAO

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups