Decoding BITWISE-OR operator.

srikanthrad
srikanthrad used Ask the Experts™
on
Error and Successful Codes
public static final int COMPLETED = 0;
public static final int BREAK = 2;
public static final int ERROR1 = 4;
public static final int ERROR2 = 8;
public static final int ERROR3 = 16;
public static final int ERROR4 = 32;

In my program, the error codes are bitor'ed like
if there is an error code and if it were to be BREAK.

returncode |= BREAK;

returncode will be 2.

and if there were to be an error code and if it were to ERROR1 and ERROR2, then

catch(Exception1 e1){

returncode |= ERROR1;

} finally {

returncode |= ERROR2;

}

as you can see in this case, the returncode will be 12. We know that it is ERROR1 and ERROR2.

How to decode this programatically?

When 12 is returned, I should be able to return "There is an Error: Error1 and Error2" in this case.

You can suggest response in any programming language, although I use Java.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
keep the error values in an array and loop it through the return value. to check a specific error is present, do (~returncode & ERR[i]) == 1
you need to change your error code values though, back each one off by 1 power of 2.  It should look like this:
public static final int COMPLETED = 0;
public static final int BREAK = 1;
public static final int ERROR1 = 2;
public static final int ERROR2 = 4;
public static final int ERROR3 = 8;
public static final int ERROR4 = 16;

then if you detect an error, you need to check for each error code using the bitwise AND operation:

if(err_flag & ERROR1)
{
...
}
if(err_flag & ERROR2)
{
...
}
and so on...


Hope this helps.

JB
or simply
public static final int COMPLETED = 0;
public static final int BREAK = 2;
public static final int ERROR1 = 4;
public static final int ERROR2 = 8;
public static final int ERROR3 = 16;
public static final int ERROR4 = 32;

...


        CString cs;
	if (returncode&BREAK)
	{
		cs += "BREAK ";
	}
	if (returncode&ERROR1)
	{
		cs += "ERROR1 ";
	}
	if (returncode&ERROR2)
	{
		cs += "ERROR2 ";
	}
	if (returncode&ERROR3)
	{
		cs += "ERROR3 ";
	}
	if (returncode&ERROR4)
	{
		cs += "ERROR4 ";
	}

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

oh, it is the same like johnbouma mentioned.

Author

Commented:
The suggestions are working, But do I need to back each one off by 1 power of 2 as suggested by johnbouma or not necessary?
another way is define the errors in enum with its corresponding values , and put the condition in a loo with enum [i] value. so the code won't be so big and don't need to mention errnum in different places. if you would like to code like that I can quote some code.

Author

Commented:
@roshmon:
enum would be great. Can you please give me sample code with enum?
      public enum ERROR_CODES {
            COMPLETED = 0,
            BREAK = 1,
            ERROR1 = 2,
            ERROR2= 4,
            ERROR3 = 8
      }; Is this how I declare enums? I am getting lot of compile time errors when I declare enum like this.
I never written java code, still did a pseudo code attempt :)

public enum ErrValues {
    COMPLETED	(0),
    BREAK		(2),
    ERROR1		(4),
    ERROR2		(8),
    ERROR3		(16),
    ERROR4		(32),
;

for (ErrValues e : ErrValues.values())
	{
		if(returncode&e)
           System.out.printf("%s ", p);
	}

Open in new window

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial