Tell me why, if you write:

public static void main(String[] args) { long i = 0xa; System.out.print(i); 

then it displays 10. And if so:

 public static void main(String[] args) { long i = 0xba; System.out.print(i); 

Does it display 186? In short, explain how the assignment of a variable's value to this way of writing works.

    1 answer 1

    The fact is that 0x means that the number will be written in hexadecimal notation.

    If you open any number converter from a hexadecimal system into a 10-bit system ( for example, here ) and enter ba you will see the result: 186

    a is the number ten in the hexadecimal system:

     0 - 0 1 - 1 2 - 2 3 - 3 4 - 4 5 - 5 6 - 6 7 - 7 8 - 8 9 - 9 a - 10 b - 11 c - 12 d - 13 e - 14 f - 15 

    Hence the corresponding result.

    Translation from 16 to 10 is carried out according to the basic rules:

     Для перевода шестнадцатеричного числа в десятичное необходимо это число представить в виде суммы произведений степеней основания шестнадцатеричной системы счисления на соответствующие цифры в разрядах шестнадцатеричного числа. Например, требуется перевести шестнадцатеричное число BA в десятичное. В этом числе 2 разряда. Разряды считаются, начиная с нулевого, которому соответствует младший бит (крайний правый). В соответствии с вышеуказанным правилом представим его в виде суммы степеней с основанием 16: BA = (11 * 16^1) + (10 * 16^0) = 11 * 16 + 10 = 176 + 10 = 186 

    • No, I understand, but tell me the principle of this override. - Miron Fisenko
    • @ MironFisenko ie How is the number system translated to another? - Alexey Shimansky
    • Yes it is. - Miron Fisenko
    • one
      @MironFisenko I described, of course, but it’s like the basics of programming, - Alexey Shimansky