|
-
December 18th, 2001, 10:36 PM
#1
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
-
December 19th, 2001, 02:45 AM
#2
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
-
December 19th, 2001, 09:27 AM
#3
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)
-
December 20th, 2001, 12:48 AM
#4
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
-
December 20th, 2001, 03:35 AM
#5
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?
-
December 20th, 2001, 07:29 PM
#6
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
-
December 23rd, 2001, 07:36 AM
#7
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 to ut << 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|