How can I mask a hex int with java?

I have an integer containing the hexa value. I want to extract the first characters from this hexa value, as it was a String value, but I do not want to convert it to String.

int a = 0x63C5;
int afterMask= a & 0xFFF;
System.out.println(afterMask); // this gives me "3C5" but I want to get the value "63C" 

In my case, I cannot use String utilities such as substring.

+4
source share
3 answers

It is important to understand that an integer is just a number. There is no difference between:

int x = 0x10;
int x = 16;

Both end with integers with the same value. The first is written in the source code as hexadecimal, but it still represents the same value.

, , , , -. , , 4-15 , 0-11 .

, :

int afterMask = (a & 0xFFF0) >> 4;

, :

int afterMask = (a >> 4) & 0xFFF;

() 1596 = (hex) 63C.

, 12+, - , () 0x1263c5, , 0x63c.

+10

"63C", 4 ( nibble). ,

int a = 0x63C5;
int afterMask = a >> 4;
System.out.println(Integer.toHexString(afterMask));

( )

63

+9
  int a = 0x63C5;
  int aftermask = a >> 4 ;     
  System.out.println( String.format("%X", aftermask) );
+2

Source: https://habr.com/ru/post/1694651/


All Articles