Thread: adding together two shorts

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    adding together two shorts

    I have 2 32-bit unsigned long (DWORD) variables. the values that I am interested are contained in the low order shorts (WORD) of both of the variables. I would like to somehow and these 2 shorts together to form a long, since I know I am dealing with unsigned variables here I don't have to worry about sign bits or anything. I tried this:

    Code:
    return LOWORD(dwVer1) & LOWORD(dwVer2);
    without any luck. when I try that I get the low order short/WORD in dwVer2 printed, but that is not what I am trying to achieve here. say for example the low order short/WORD in dwVer1 is 1, and the low order short/WORD in dwVer2 is 2, I am looking for the value 12 to be returned.

    would anyone here please be able to help me out here? I obviously don't have much experience with bit manipulation in C, infact I think I will go back and review it again a few times. it's one of those boring topics that I probably went through too quickly, and now I'm regretting it.

    any help is appreciated. thanks.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'd shift one of the shorts 32 bits to the left and OR it with the other one. I'll give an example with chars into a short for readability, with spaces between each byte.
    Code:
    char1 = 00110111
    char2 = 11010101
    short = (00110111 << 8) | 11010101
          = 00110111 00000000 | 11010101
          = 00110111 11010101
    In short (no pun intended):
    Code:
    short x = rand();
    short y = rand();
    long both = x << 32 | y;
    [edit]
    without any luck. when I try that I get the low order short/WORD in dwVer2 printed, but that is not what I am trying to achieve here. say for example the low order short/WORD in dwVer1 is 1, and the low order short/WORD in dwVer2 is 2, I am looking for the value 12 to be returned.
    That's not the result you'd get, just so you know. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well to join them together, it would be
    return LOWORD(dwVer1) | ( LOWORD(dwVer2) << 16 );

    To separate them, use >> 16, and mask the result with & 0xFFFF
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    holy fast reply dwks.

    I tried this:

    Code:
    dwRet = osvi.dwMajorVersion << 32 | osvi.dwMinorVersion;
    wsprintf(buf, "&#37;d", dwRet);
    MessageBox(NULL, buf, NULL, MB_OK);
    but it still prints 1. :/

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    See Salem's post. I misread yours, thinking you just had two shorts you wanted stored in a long. Plus I said 32 instead of 16. Oops.

    To use only the lower 16 bits of a variable and zero out the upper 16, use
    Code:
    variable & SHRT_MAX
    Then you'd shift one short 16 bits to the left again.
    Code:
    unsigned long x = rand();
    unsigned long y = rand();
    unsigned long both = ((x & USHRT_MAX) << 16) || (y & USHRT_MAX);
    USHRT_MAX is usually 65535 (which you can use instead) and it's in <limits.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    this prints 5:

    Code:
    wsprintf(buf, "&#37;d", LOWORD(osvi.dwMajorVersion));
    MessageBox(NULL, buf, NULL, MB_OK);
    this prints 1:

    Code:
    wsprintf(buf, "%d", LOWORD(osvi.dwMinorVersion));
    MessageBox(NULL, buf, NULL, MB_OK);
    so I'm looking for 51.

    this prints 65541:

    Code:
    dwRet = LOWORD(osvi.dwMajorVersion) | ( LOWORD(osvi.dwMinorVersion) << 16 );
    wsprintf(buf, "%d", dwRet);
    MessageBox(NULL, buf, NULL, MB_OK);
    this prints 1:

    Code:
    dwRet = ((osvi.dwMajorVersion & 65535) << 16) || (osvi.dwMinorVersion & 65535);
    wsprintf(buf, "%d", dwRet);
    MessageBox(NULL, buf, NULL, MB_OK);
    :/

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right . . . well, what you're looking for can't be easily done with bit shifting. You'll need to do something like this.

    Look at the edit at the end before you wade through this post.

    Count the number of digits in one number: "1" has one digit, "12", has two digits, and "12345" has five. Then multiply the other number by 10^digits and add them together. It's a lot easier than it sounds. You can count the digits much like this:
    Code:
    long num = rand(), digits = 0;
    while(num) {
        num /= 10;
        digits ++;
    }
    I'm sure you can code it from there.

    Or use strings. You could use sprintf() easily to concatenate the numbers:
    Code:
    char both[BUFSIZ];
    sprintf(both, "&#37;d%d", x, y);
    Then you could convert both back into a number, with sscanf() or strtol() or something else. I like strtol(), but that's just me.
    Code:
    long bothnum = strtol(both, NULL, 0);
    It's in <stdlib.h>, I think. You'd have to check.

    Or, if the numbers are always one digit, just use
    Code:
    long result = x*10 + y;
    [edit] Bah, it's simple! Just go
    Code:
    wsprintf(buf, "%d%d", LOWORD(osvi.dwMajorVersion), LOWORD(osvi.dwMinorVersion));
    Will that work? *printf() is just like printf(). You can have multiple format specifiers. [/edit]
    Last edited by dwks; 06-11-2007 at 04:04 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with storing the last value of a loop and adding it?
    By colmustang in forum C Programming
    Replies: 4
    Last Post: 03-08-2008, 09:31 AM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  4. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  5. Java: Sorting and Adding a Row in Table
    By alphaoide in forum Tech Board
    Replies: 4
    Last Post: 08-12-2005, 08:22 PM