CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2001
    Posts
    17

    Is this legal piece of code

    Hi

    unsigned long status_code =0x00000000;

    enum codes { code1=0x00000080, code2=0x00000800, code3=0x00008000 };
    code status_code1=code1;
    code status_code2=code2;
    code status_code3=code3;


    status_code= status_code | status_code1;
    status_code= status_code | status_code2;
    status_code= status_code | status_code3;

    What should we expect status_code be I guess 0x0008880?
    Can it be simplified?
    How can I I achive same code_status output if my enum code is
    enum codes { code1=0x80, code2=0x800, code3=0x8000 };


    Thanks,
    Ruhail



  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Is this legal piece of code

    status_code |= (status_code1 | status_code2 | status_code3);

    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Is this legal piece of code

    Almost legal - your enumeration is called codes but your variables were called code.

    Also you could make it more efficient by implementing as Igor suggested

    In addition, x |= y; is equivalent and possibly more efficient than x = x | y; (possibly because a compiler may detect and correct the inefficiency)




  4. #4
    Join Date
    Nov 2001
    Posts
    17

    Re: Is this legal piece of code

    Thanks for your response. I am wonder why the last value in this example is negative. If enum holds only int , it should have stopped at 65535.I can't effort to loose the last value. Any other suggestion other than using enum.
    Basicillay My requirement is to send status_code as strings over the corba link, and status_codes can be any from 0x00000000 to 0xFFFFFFFF
    But 0x80000000 being negative means I can't send some status values as their real values. Also How can I convert these enum values to strings, if I will decide to use enum. I will really appreciate your response.

    #include <iostream.h>
    void main(void)
    {

    enum status_code { code1=0x00000008,
    code2=0x00000080,
    code3=0x00000800,
    code4=0x00008000,
    code5=0x00080000,
    code6=0x00800000,
    code7=0x08000000,
    code8=0x80000000,
    };
    status_code my_code1=code1;
    status_code my_code2=code2;
    status_code my_code3=code3;
    status_code my_code4=code4;
    status_code my_code5=code5;
    status_code my_code6=code6;
    status_code my_code7=code7;
    status_code my_code8=code8;

    cout << endl << my_code1 << endl;
    cout << endl << my_code2 << endl;
    cout << endl << my_code3 << endl;
    cout << endl << my_code4 << endl;
    cout << endl << my_code5 << endl;
    cout << endl << my_code6 << endl;
    cout << endl << my_code7 << endl;
    cout << endl << my_code8 << endl;

    }

    Output

    8

    128

    2048

    32768

    524288

    8388608

    134217728

    -2147483648


  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Is this legal piece of code

    you need to cast to an unsigned int (or long)

    why are you using only 8 of the 32 bits?


  6. #6
    Join Date
    Nov 2001
    Posts
    17

    Re: Is this legal piece of code

    I can't used other bits they are reserved. Can you please write more about casting it to long or int. Do you mean to turn the last negative value to positive I have cast it to long or unsigned long.
    Thanks,
    Bashir


  7. #7
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Is this legal piece of code

    The last number is printed as negiative because enums are inherently based on signed ints. It should print the correct value by changing the code tout << endl << (unsigned) my_code8 << endl;

    Note that this only affects how it is displayed. Regardless if whether it's "-2147483648" (as a negative signed number), or "2147483648" (as a positive unsigned number), the bit image of the number is always 0x80000000.



    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

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

Featured