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. General Programming
  3. C / C++ / MFC
  4. Storing 2 longs in a single variable

Storing 2 longs in a single variable

Scheduled Pinned Locked Moved C / C++ / MFC
question
16 Posts 8 Posters 0 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.
  • G Gilfrog

    I don't think that will work because that stores it in a structure. I need it to be in one variable.

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #3

    use an unsigned __int64, something like: unsigned __int64 i64 = (unsigned long)longOne * ((unsigned long)-1) + longTwo; ie.. the high 4 bytes are your first long and the low four are your 2nd long. -c


    All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public

    War Pigs

    G 1 Reply Last reply
    0
    • T Tomasz Sowinski

      use std::pair. Tomasz Sowinski -- http://www.shooltz.com

      *** Si fractum non sit, noli id reficere. ***

      G Offline
      G Offline
      Gilfrog
      wrote on last edited by
      #4

      I don't think that will work because that stores it in a structure. I need it to be in one variable.

      C T 2 Replies Last reply
      0
      • G Gilfrog

        I don't think that will work because that stores it in a structure. I need it to be in one variable.

        T Offline
        T Offline
        Tomasz Sowinski
        wrote on last edited by
        #5

        Gilfrog wrote: I need it to be in one variable. You may use __int64 if you want to play with shifting bits. But why can't you use a structure? Tomasz Sowinski -- http://www.shooltz.com

        *** Si fractum non sit, noli id reficere. ***

        1 Reply Last reply
        0
        • C Chris Losinger

          use an unsigned __int64, something like: unsigned __int64 i64 = (unsigned long)longOne * ((unsigned long)-1) + longTwo; ie.. the high 4 bytes are your first long and the low four are your 2nd long. -c


          All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public

          War Pigs

          G Offline
          G Offline
          Gilfrog
          wrote on last edited by
          #6

          How do i get the two longs back out of the unsigned__int64?

          C 1 Reply Last reply
          0
          • G Gilfrog

            How can i store 2 longs in a single variable. Similare to storing 2 shorts in a long with the MAKELONG macro. Can i do this with Long64? Thanks

            R Offline
            R Offline
            Ryan B
            wrote on last edited by
            #7

            A structure would solve the issue would it not? #pragma pack(1) typedef struct two_longs_dont_make_a_right { long one; long erone; } two_longs; ? Why in 'one' variable? Ryan Baillargeon Software Specialist Fuel Cell Technologies Inc.

            A 1 Reply Last reply
            0
            • R Ryan B

              A structure would solve the issue would it not? #pragma pack(1) typedef struct two_longs_dont_make_a_right { long one; long erone; } two_longs; ? Why in 'one' variable? Ryan Baillargeon Software Specialist Fuel Cell Technologies Inc.

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #8

              I need to save an Array of 2 shorts and long to a text file. I just figured it would be easier to save them in one variable that to seperate them with commas or something like that.

              C 1 Reply Last reply
              0
              • A Anonymous

                I need to save an Array of 2 shorts and long to a text file. I just figured it would be easier to save them in one variable that to seperate them with commas or something like that.

                C Offline
                C Offline
                Chris Richardson
                wrote on last edited by
                #9

                Why not use a structure in combination with a union, like so:

                typedef struct tagTwoLongs
                {
                union
                {
                struct
                {
                long Long1;
                long Long2;
                };
                __int64 OneVariable;
                };
                }TwoLongs;

                Now you can refer to the two longs separately, via Long1 and Long2, or together, via OneVariable. Chris Richardson

                S P 2 Replies Last reply
                0
                • C Chris Richardson

                  Why not use a structure in combination with a union, like so:

                  typedef struct tagTwoLongs
                  {
                  union
                  {
                  struct
                  {
                  long Long1;
                  long Long2;
                  };
                  __int64 OneVariable;
                  };
                  }TwoLongs;

                  Now you can refer to the two longs separately, via Long1 and Long2, or together, via OneVariable. Chris Richardson

                  S Offline
                  S Offline
                  Scott H Settlemier
                  wrote on last edited by
                  #10

                  Make sure that the pack pragma is 4 or less for the union technique to work. You might want to ensure this by temporarily specifying the packing alignment: #pragma pack(push,4) typedef struct tagTwoLongs { union { struct { long Long1; long Long2; }; __int64 OneVariable; }; }TwoLongs; #pragma pack(pop) P.S. Is there a way to get a compile time assertion that sizeof(long)==4?

                  C G 2 Replies Last reply
                  0
                  • S Scott H Settlemier

                    Make sure that the pack pragma is 4 or less for the union technique to work. You might want to ensure this by temporarily specifying the packing alignment: #pragma pack(push,4) typedef struct tagTwoLongs { union { struct { long Long1; long Long2; }; __int64 OneVariable; }; }TwoLongs; #pragma pack(pop) P.S. Is there a way to get a compile time assertion that sizeof(long)==4?

                    C Offline
                    C Offline
                    Chris Richardson
                    wrote on last edited by
                    #11

                    Good call on the packing. I forgot about that. Scott H. Settlemier wrote: P.S. Is there a way to get a compile time assertion that sizeof(long)==4? Yeah, in winnt.h, there's a macro called C_ASSERT. Here's it's definition, and the comment that describes it:

                    //
                    // C_ASSERT() can be used to perform many compile-time assertions:
                    // type sizes, field offsets, etc.
                    //
                    // An assertion failure results in error C2118: negative subscript.
                    //

                    #define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

                    Chris Richardson

                    S 1 Reply Last reply
                    0
                    • C Chris Richardson

                      Good call on the packing. I forgot about that. Scott H. Settlemier wrote: P.S. Is there a way to get a compile time assertion that sizeof(long)==4? Yeah, in winnt.h, there's a macro called C_ASSERT. Here's it's definition, and the comment that describes it:

                      //
                      // C_ASSERT() can be used to perform many compile-time assertions:
                      // type sizes, field offsets, etc.
                      //
                      // An assertion failure results in error C2118: negative subscript.
                      //

                      #define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

                      Chris Richardson

                      S Offline
                      S Offline
                      Scott H Settlemier
                      wrote on last edited by
                      #12

                      ooh, nice. thanks. I've been wanting that ability for a long time. I see they just sorta kludged one up there. (neg index error. :laugh:) Too bad this wasn't added to the language.

                      1 Reply Last reply
                      0
                      • S Scott H Settlemier

                        Make sure that the pack pragma is 4 or less for the union technique to work. You might want to ensure this by temporarily specifying the packing alignment: #pragma pack(push,4) typedef struct tagTwoLongs { union { struct { long Long1; long Long2; }; __int64 OneVariable; }; }TwoLongs; #pragma pack(pop) P.S. Is there a way to get a compile time assertion that sizeof(long)==4?

                        G Offline
                        G Offline
                        Gilfrog
                        wrote on last edited by
                        #13

                        thanks, that works for me.

                        1 Reply Last reply
                        0
                        • C Chris Richardson

                          Why not use a structure in combination with a union, like so:

                          typedef struct tagTwoLongs
                          {
                          union
                          {
                          struct
                          {
                          long Long1;
                          long Long2;
                          };
                          __int64 OneVariable;
                          };
                          }TwoLongs;

                          Now you can refer to the two longs separately, via Long1 and Long2, or together, via OneVariable. Chris Richardson

                          P Offline
                          P Offline
                          PJ Arends
                          wrote on last edited by
                          #14

                          You mean the Win32 LARGE_INTEGER structure defined in WinNT.h


                          CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

                          C 1 Reply Last reply
                          0
                          • P PJ Arends

                            You mean the Win32 LARGE_INTEGER structure defined in WinNT.h


                            CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

                            C Offline
                            C Offline
                            Chris Richardson
                            wrote on last edited by
                            #15

                            I only replied to him an idea and a simple definition. I suppose I could have used (and would have used) LARGE_INTEGER as my example, but I didn't think of it at the time. Chris Richardson

                            1 Reply Last reply
                            0
                            • G Gilfrog

                              How do i get the two longs back out of the unsigned__int64?

                              C Offline
                              C Offline
                              Chris Losinger
                              wrote on last edited by
                              #16

                              longOne = i64 / (unsigned long)-1; longTwo = i64 - (longOne * (unsigned long)-1); or something similar -c


                              All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public

                              War Pigs

                              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