//Маскировка доп. знаков разряда Class Hexbyte { Static public void main (String arms[]){ char hex[] ={ '0','1'.'2','3','4','5','6','7' '8','9','a','b','c','d','e','f' }; Byte b = (byte) 0xf1 System.out.println("b=0x" + hex[(b>>4) & 0x0f] + hex[b&0x0f]); } } Result:b=0xf1 
  • @manh. To format a code, select it with the mouse and click on the {} button of the editor. - fori1ton
  • one
    It seems to me, or was the identical question literally over the last week? I could not find it. - etki
  • one
    @Etki so the students are given the same tasks, or similar) - DreamChild
  • @Elki, the fact is that I have been studying java on my own and have been stuck on this moment for 2 days already (I asked this question the day before yesterday), apparently, my question was deleted. Therefore, I decided to ask the question again because I did not receive a reply. - roma_fam
  • @DreamChild in general, if not difficult to explain, please, why in this case does static stand up to public? and I can’t understand the meaning of b >> 4 and the action on it in System.out.println I will be very grateful for the help) - roma_fam

1 answer 1

Translate all the numbers in binary form and then you will understand everything.

0xf1 in the binary representation is 11110001. Shift it by 4 characters to the right (b >> 4), we get 00001111. It turns out that with this action we "take" from among only the high byte (0xf).

By the operation b & 0x0f we get the low byte, since 0xf1 & 0x0f = 11110001 & 00001111 = 00000001

  • @Mashin Thank you! I figured it out, but can you also explain why static preposes public? And how did you translate 0xf1 getting 11110001 (when I tried it myself, I immediately translated the number 15 into binary-00001111- this caused confusion ... well, just bother so much) The last thing that interests me is: Do I always need to specify the character data type with an array of symbols in the form of hexadecimal variables when we deal with hexadecimal values? You are sorry for the intrusiveness, it has hurt a lot of questions lately) Thank you in advance! - roma_fam
  • 0xf1 is not 15, but 241. In general, according to this table, it is easier to translate rapidtables.com/convert/number/how-hex-to-binary.htm or any engineering calculator. static can be written and after public, I think there is no difference. The last question is not very clear to me. - Boris Timofeev
  • @Mashin understood, in general, is it always necessary to specify all these array elements of type char? - roma_fam