Unable to obtain long value from bitwise operation

0

I need to obtain the output of the following bitwise operation for further processing. Unfortunately anytime I attempt to convert the output to int or long types I get a Number format Exception. How do I ensure the output of a type I can process?

String TYPE = "type";
long TOTAL_MASK = 0xFFFFFFFF;
long BASE_INBOX_TYPE = 20;
long BASE_TYPE_MASK = 0x1F

long type = Integer.valueOf("" + TYPE + " & " + (TOTAL_MASK - BASE_TYPE_MASK) + " | " + BASE_OUTBOX_TYPE + "");
java
bitmap
bit-manipulation
bitwise-operators
bitmask
asked on Stack Overflow Jan 17, 2019 by samuel owino • edited Jan 17, 2019 by Karol Dowbecki

2 Answers

2

Integer.valueOf(String) is used to parse a number literal from text, not to evaluate arbitrary expression. As per method javadoc:

The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method.

You need to write your expression as code e.g.:

long type = (TOTAL_MASK - BASE_TYPE_MASK) | BASE_OUTBOX_TYPE;
answered on Stack Overflow Jan 17, 2019 by Karol Dowbecki • edited Jan 17, 2019 by Karol Dowbecki
1

Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type and if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int by a widening primitive conversion (ยง5.1.2).

String TYPE = "type";
long TOTAL_MASK = 0xFFFFFFFF;
long BASE_INBOX_TYPE = ~20; // (~)promotion to int first (not necessary)
long BASE_TYPE_MASK = 0x1F;

long type = (TOTAL_MASK - BASE_TYPE_MASK) | BASE_INBOX_TYPE;

System.out.println(type);
answered on Stack Overflow Jan 18, 2019 by corneliouz Bett

User contributions licensed under CC BY-SA 3.0