-2

I know int is a data type used to declare variables, but it is in the decimal system. What if I want to specify that my integer is in binary?

Edit: I am sorry if this question is being confused with the one that it was marked with as a duplicate. To clarify:

There is a data type int in java that specifies integer literals. Integer literals come in various number systems, including decimal, hexadecimal, octal, binary. When we declare a variable to be an integer, we write: int var;

We can give var a binary integer value by doing an assignment like: var = 0b1101

However, I was asking is it possible to specify the integer to be in binary system (i.e. a more specific data type of integer) without later having to put 0b in front of the binary number we assign later to var?

Sandy
  • 81
  • 2
  • 4
    Your assumption that "int is a decimal type" is wrong. `int` is a data type for numbers. These numbers can be written in decimal (123) or binary (0b01111011), but that's only how they are written. How they are actually stored in the computer's memory is entirely different. – Roland Illig Jan 05 '19 at 04:56
  • _"is it possible to specify the integer to be in binary system (i.e. a more specific data type of integer)"_ there is no such thing. Integers are integers in Java, and they are stored in 4 bytes encoded in one's-complement binary format. – Mark Rotteveel Jan 05 '19 at 16:30
  • So you mean some kind of a - more or less global - setting that assumes all assignments to `int`s are done from *binary presentations* of that integer? Like `int one = 1;`, `int two = 10;`, `int three = 3;`, oops... – pirho Jan 05 '19 at 18:20
  • @pirho Yes that was what I was referring to, but Mark Rotteveel helped clarify that there is no such thing. Thanks! – Sandy Jan 06 '19 at 02:59

1 Answers1

3

Yes. To write a binary literal you write a number zero and then a letter b like

int i = 0b10;
System.out.printf("There are %d kinds of people...%n", i);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249