Click to See Complete Forum and Search --> : Is this legal piece of code


ruhail
December 18th, 2001, 09:36 PM
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

Igor Soukhov
December 19th, 2001, 01:45 AM
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)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Russian Software Developer Network http://rsdn.ru

NMTop40
December 19th, 2001, 08:27 AM
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)

ruhail
December 19th, 2001, 11:48 PM
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

NMTop40
December 20th, 2001, 02:35 AM
you need to cast to an unsigned int (or long)

why are you using only 8 of the 32 bits?

ruhail
December 20th, 2001, 06:29 PM
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

James Curran
December 23rd, 2001, 06:36 AM
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 to:out << 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.