Thread: how achieve split of unsigned4 into two signed2's using bit-wise instead of union?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    how achieve split of unsigned4 into two signed2's using bit-wise instead of union?

    I know this code is completely valid but in the interest of learning, how would I achieve the same thing as below using bitwise operations on the unsigned4 value instead of the union "trick"?

    Code:
    void value(unsigned4 v)
    {
        union 
        {
            struct
            {
            signed2 left;
            signed2 right;
            } split;
     
            unsigned4 combined;
     
        } trick;
     
        //Set the base value
        trick.combined = v;
     
        //"Split" the value into left and right values
        left = trick.split.left;
        right = trick.split.right;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What are signed2 and unsigned4?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    Code:
    left = (v & 0xFFFF0000) >> 16;
    right = (v & 0x0000FFFF);
    perhaps?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by master5001 View Post
    What are signed2 and unsigned4?
    As best I can gather from the code:

    unsigned4 = unsigned int
    signed2 = signed short int

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Waterbottle View Post
    Code:
    left = (v & 0xFFFF0000) >> 16;
    right = (v & 0x0000FFFF);
    perhaps?
    It is sometimes better to do
    Code:
    left = (v  >> 16) & 0x0000FFFF;
    right = v & 0x0000FFFF;
    because using smaller constants and put the and operation last makes it possible for the compiler to figure out that those operations are meaningless if the processor has native support for 16-bit integers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    It is sometimes better to do
    Code:
    left = (v  >> 16) & 0x0000FFFF;
    right = v & 0x0000FFFF;
    because using smaller constants and put the and operation last makes it possible for the compiler to figure out that those operations are meaningless if the processor has native support for 16-bit integers.
    Yeah I recommend always doing a right shift before the masking. On CISC systems, it takes fewer bytes of compiled code to store the smaller constant in the compiled instructions. (assuming it isn't exactly the case matsp mentioned)

    Nothing wrong either way of course, it's just a micro optimisation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing bit fields using a union problem.
    By samdomville in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 04:39 PM
  2. bit fields and union
    By azerej in forum C Programming
    Replies: 4
    Last Post: 05-26-2008, 06:33 PM
  3. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM