0

I am familiar with binary representation of integers in memory - i.e. how to represent positive numbers, negative numbers, the leading bit is for the sign and so on. I have no idea what representation like 0x000f means and how should I interpret it. Can someone give some nice tutorials or at least tell me what to google, since when I google "binary representation" I dont find any explanation of what this number 0x000f means?

sammy333
  • 1,384
  • 6
  • 21
  • 39

3 Answers3

3

The example you gave 0x000F is not actually binary; it is hexadecimal (base 16). You can tell since hexadecimal numbers usually begins with 0x, while binary will often begin with 0b (if it begins with anything at all).

In the hexadecimal base system, each digit can have one of the following values:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Where A represents 10, B represents 11, C represents 12, D represents 13, E represents 14, and F represents 15.

So, 0x000F would be equal to 0 * 16^3 + 0 * 16^2 + 0 * 16^1 + 15 * 16^0 = 15

Here is a resource to look at for more info: https://en.wikipedia.org/wiki/Hexadecimal

Nick Meyer
  • 1,771
  • 1
  • 17
  • 29
1

There are different prefixes in java to assign an integer value. 0 for octadezimal 0b for binary and 0x for hexadezimal.

    int binary  = 0b10;
    int octa    = 010;
    int hexa    = 0x10;

    System.out.println(binary);
    System.out.println(octa);
    System.out.println(hexa);

Output:

2
8
16

Hexdezimal is base 16 so you need 16 different signs. From 0 to 9 and for A to F. A is equal to 10 in dezimal and so on. If you need to assign an integer in any other number system which has no literal representation in java, you can use Integer.parse("1011",2) which takes as secound argument the base of the number system.

kai
  • 6,702
  • 22
  • 38
  • Thanks! This is what I wanted to know - how should I read these numbers. These prefixes are not clear to me. Thanks again! – sammy333 Aug 12 '14 at 08:08
  • 1
    Maybee this is interesting for you http://stackoverflow.com/questions/2670639/why-are-hexadecimal-prefixed-as-0x – kai Aug 12 '14 at 08:16
0

From Hexadecimal:

Definition:

Hexadecimal refers to a numbering system that has a base of 16. This means it uses all the numerals 0,1,2,3,4,5,6,7,8,9 as well as the letters A,B,C,D,E,F for each digit of a number. In a similar way that a language such as Italian represents the English word 'number' as 'numero', hexadecimal represents the denary number of 122 as 7A. It's just another system to represent the same value.

To show the progression of the hexadecimal numbering system, look at the hexadecimal values below as they are converted from denary numbers. Note that once it gets to ten the hexadecimal system uses letters:

Denary=Hexadecimal;0=0;1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=A;11=B;12=C;13=D;14=E;15=F;16=10; and so on.

The hexadecimal system is a compact way to represent bits. Each hexadecimal digit can represent four bits i.e., a hexadecimal digit can represent 16 values (0-15), this is exactly the same as four bits (1111). Due to this perfect conversion and that modern day computers are designed around the byte (8 bits), hexadecimal is a widely used method to represent binary numbers.

Examples:

In java, to represent a hexadecimal number as a literal use a leading zero followed by an x.

e.g., The following will return the value of 122 for the hexadecimal number 7A:

int hexNumber = 0x7A;
System.out.println(hexNumber); 
DavidPostill
  • 7,734
  • 9
  • 41
  • 60