My simple question is why:
System.out.println(010|4);
prints "12"? I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number?
A leading 0 denotes an octal numeric value so the value 010 can be decoded thus: 010 = 1 * 81 + 0 * 80 = 8
Have a look at the Java Language Specification, Chapter 3.10.1 Integer Literals
An integer literal may be expressed in decimal (base 10), hexadecimal
(base 16), octal (base 8), or binary (base 2).
[...]
An octal numeral consists of an ASCII digit 0 followed by one or more
of the ASCII digits 0 through 7 interspersed with underscores, and can
represent a positive, zero, or negative integer.
Now you should understand why 010 is 8.
That is because java takes it as an octal literal and hence produces 12. Try System.out.println(10|4) and the result is 14. Because this time it is taken as decimal literal.
As everybody mentioned here that 010 is an Octal Integer literal . The leading 0 specifies that it is an octal representation . Actual value will be :
1*8^1 + 0*8^0 = 8(decimal) = 1000(binary-only last 4 digits)
Now coming back to the SOP :
System.out.println(010|4);
Applying Bitwise OR on 010 and 4(considering only the last 4 digits) =>
1000|0100
= 1100
= 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 8 + 4 + 0 + 0
= 12(decimal)
Any number in Java which fulfill the below Conditions -
A. number Should have three or More Digital
B.Number should Start with 0.
If above Condition are true then number treated as Octal_Base (8) Number.
Therefore,
010=(8^2)*0+(8^1)*1+(8^0)*0=64*0+8*1+1*0=8
So,
010=8
One point you should consider that the number will be in octal if "0XX" i.e. both X are in range [0,7], otherwise it will result in "Integer number too large".
Related
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 6 years ago.
See the following example:
int a = 0111;
System.out.println(a);
Output: 73
Why is this happening and how can i get the exact value without conversion?
According to the Java Language Specification 3.10.1. Integer Literals, there are 4 ways to write an integer literal:
DecimalNumeral: Digit 1-9 followed by zero or more digits 0-9, or the number 0.
HexNumeral: Leading 0x followed by one or more hexadecimal digits (0-9, a-f). Not case-sensitive.
OctalNumeral: Digit 0 followed by one or more digits 0-7.
BinaryNumeral: Leading 0b followed by one or more digits 0 or 1. Not case-sensitive.
(All 4 allow underscores for clarity)
As you can see, the number 111 depends on the prefix:
0111 (OctalNumeral) is 1*8*8 + 1*8 + 1 = 64+8+1 = 73.
111 (DecimalNumeral) is 1*10*10 + 1*10 + 1 = 100+10+1 = 111.
0b111 (BinaryNumeral) is 1*2*2 + 1*2 + 1 = 4+2+1 = 7.
0x111 (HexNumeral) is 1*16*16 + 1*16 + 1 = 256+16+1 = 273.
Which one is right depends on what you wanted.
Well you're getting the precise value. The problem is the format.
0111
gets interpreted as octal value in java. So your value actually is 73 as a quick calculation would show.
A octal value in java is defined this way:
OctalNumeral:
0 OctalDigits
0 Underscores OctalDigits
jls 3.10.1
So 0_111 would be interpreted in the same way.
The format to use in java code would be:
0b111
Which actually gets interpreted as 7 - in binary format - , as expected.
I'll add the links later on, I'm in a bit of a hurry.
class test{
public static void main(String args[]){
int a = 011;
System.out.println(a);
}
}
Why I am getting 9 as output instead of 011?
How can I get 011 as output?
The JLS 3.10.1 describes 4 ways to define integers.
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7 ...
A decimal numeral is either the single digit 0, representing the integer zero, or consists of an digit from 1 to 9 optionally followed by one or more digits from 0 to 9 ...
In summary if your integer literal (i.e. 011) starts with a 0, then java will assume it's an octal notation.
Solutions:
If you want your integer to hold the value 11, then don't be fancy, just assign 11. After all, the notation doesn't change anything to the value. I mean, from a mathematical point of view 11 = 011 = 11,0.
int a = 11;
The formatting only matters when you print it (or when you convert your int to a String).
String with3digits = String.format("%03d", a);
System.out.println(with3digits);
The formatter "%03d" is used to add leading zeroes.
Alternatively, you could do it in 1 line, using the printf method.
System.out.printf("%03d", a);
A numeric literal that starts with 0 is parsed as an octal number (i.e. radix 8). 011 is 9 in octal.
The 011 is being interpreted as an octal number. This means it's in base 8. See also this SO post. From the accepted answer by Stuart Cook:
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity.
Digits in Base 10:
100s, 10s, 1s
Digits in Base 8:
64s, 8s, 1s
So the 011 is interpreted as 8+1=9
Is
00, 000, or 000...
an integer in Java?
Or is it an octal? If is it an octal is
001 or 0005
an octal?
All are integers, but...
1 is decimal
0 is decimal
01 is octal
00 is octal
From Java Language Specification (emphasis mine):
Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral - not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.
Number literal representation examples in Java will give you the answer:
int decimal = 100;
int octal = 0144;
int hex = 0x64;
int binary = 0b1100100;
So 00, 000 and 0000 are all octal(base-8) integers.
Directly from specification
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
You can check 0 though 7
byte x=3;
x=(byte)~x;
System.out.println(x);
I was confused between the output being 4 or 0 but the output is coming to be -4.
How is that ??
It's simple !!
~a = -a - 1
Since the value of a = 3, the answer must be -4. You can check with other values too.
On a byte 3 is represented as 00000011. Then ~3 is 11111100 which is a negative number (starts with 1).
See the official docs:
The unary bitwise complement operator "~" inverts a bit pattern; it
can be applied to any of the integral types, making every "0" a "1"
and every "1" a "0". For example, a byte contains 8 bits; applying
this operator to a value whose bit pattern is "00000000" would change
its pattern to "11111111".
What is 3 in binary? It's 0000 0011.
What's ~3? It's 11111100, which is -4 (Two's complement).
Note, ~ is not an negation, it's an operator.
bit 3 = 0000 0011
x = 3
~x = 1111 1100
the ~ is the bitwise not. So when the first value in the bit string is a 1 it is a negative
~ is reversing a Bit operator.
Bit reversal consists of reversing the value of a bit. If the box contains 1, you can reverse it to 0. If it contains 0, you can reverse it to 1. To support this operation, the Java language provides the bitwise negation operator represented with the ~ symbol.
3 --> 00000011
~3 --> 11111100
As it's a 2's complement number, the first bit being one means that it's a negative number.
11111100=-4
To verify see below:
00000011 <- reversing
+ 1<- adding 1
00000100 =4
I understand this:
35%10 returns 5
but, why does 000000035%10 return 9?
Ruby does the same thing. I checked with irb.
Should I strip the 0s that are padding the number? What is the java function to do that?
When you put the leading zeros followed by octal digits (from 0 to 7), you're creating an octal literal. And since 000000035 in octal is 29 in decimal, its remainder with 10 is 9.
UPDATE
If you have your "octal literal" in a string, simply use Integer.parseInt and you'll get it parsed as a decimal.
assert Integer.parseInt("035") == 35;
Leading zero in number literal means octal number.
000000035 = 035 = 3*8 + 5 = 29
29 % 10 = 9
See this page for other available formats.