How to match that these 2 values are same? [duplicate] - java

This question already has answers here:
long value with 0 on left
(5 answers)
Closed 5 years ago.
class LetsComp {
public static void main(String[] args) {
int a = 10, b = 0010;
System.out.println(a == b); // this gives false, even if both values in actual are same
}
}

In java 10 and 0010 are not the same.
0010 is in octal equivalent to 8 (in decimal), while 10 is already in decimal format.
From JLS:
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.

Related

How can I pad a hexadecimal digit with 0 in java? [duplicate]

This question already has answers here:
Integer to two digits hex in Java
(7 answers)
Closed 1 year ago.
I'm trying to pad a hexadecimal digit with 0's in the beginning so that the length of it is always 4. For example, the FF is going to be padded as 00FF. I have tried to use String.format("%04d", number) but it didn't work as my hexadecimal digit is actually a string.
I have also tried using StringUtils.leftPad() but for some reason, IntelliJ couldn't detect it.
Please note my count is going to change, but I haven't represented it here as I'm yet to implement it. This is my sample code. Thanks.
public static void main(String[] args) {
int count = 0;
String orderID = Integer.toHexString(count).toUpperCase();
System.out.println(String.format("%04d", Integer.valueOf(orderID)));
}
String.format("%04x", 255)
with output
00ff
(or use X for uppercases hex digits)
As #user16320675 point, more info about Formatter here.

how to toggle the bits of binary representation of a number? [duplicate]

This question already has answers here:
Creating a "logical exclusive or" operator in Java
(19 answers)
Closed 5 years ago.
Given a number, i want to toggle the bits of number 'n'
say for example if n = 6 -> 0110
i want to get, result = 9 -> 1001
toggle , i.e
convert 1 to 0 & convert 0 to 1 in the binary representation
how to do this programmatically in java, by doing XOR of num with 1's
Thanks all for answers, so it depends of how many bits i want to toggle
say for ex, if its 8 bits then 0xff
if its entire 32 bits then oxffffffff
1 way would be n ^ ~0
You could do exactly that.
int y = 6^0xf
If you want to use the full int, then you have to XOR with -1 (twos complement means this is all 1's).
int y = 6^-1;
You can also use a different representation to make writing it out more intuitive.
int mask = 0xffffffff; //also -1
public static void main(final String[] args) {
final int n = 0b0110;
System.out.println(Integer.toBinaryString(n));
final int m = n ^ 0xffffffff;
System.out.println(Integer.toBinaryString(m));
}

"The literal 0008 of type int is out of range" [duplicate]

I am trying to write following code.but it gives me error kindly help me.
int six=06;
int seven=07;
int abc=018;
int nine=011;
System.out.println("Octal 011 ="+nine);
System.out.println("octal O18 =" + abc);
why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.
Why this happen? what's the reason behind this Kindly tell me.
I got Following error
integer number too large: 018
int eight=018;
Octal is base-8 number system, so it means digit can be from 0 to 7, you can't use digit 8 (and 9 too) in octal number system.
// Decimal declaration and possible chars are [0-9]
int decimal = 495;
// HexaDecimal declaration starts with 0X or 0x and possible chars are [0-9A-Fa-f]
int hexa = 0X1EF;
// Octal declaration starts with 0 and possible chars are [0-7]
int octal = 0757;
// Binary representation starts with 0B or 0b and possible chars are [0-1]
int binary = 0b111101111;
If the number is string format then you can convert it into int using the below
String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
: Integer.decode(text);
why i cant give 018 and 019 to variable.
Because an integer literal prefixed with 0 is treated as octal, and '8' and '9' aren't valid octal digits.
From section 3.10.1 of the JLS:
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.
Trying to use '8' in an octal number is like trying to use 'G' in hex... it's simple not part of the set of symbols used in that base.
Octal numbers (base 8) can only use the following figures: 01234567. The same way that decimal numbers (base 10) can only use 0123456789.
So in octal representation, 17 + 1 is 20.
The prefix 0 indicates octal(8 base)(digits 0-7).
public class MainClass{
public static void main(String[] argv){
int intValue = 034; // 28 in decimal
int six = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9
System.out.println("Octal 010 = " + eight);
}
}
why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.
The leading zero signifies an octal literal. However, 8 and 9 are not valid octal digits. This makes 018 and 019 invalid.
When an integer literal starts with 0 in Java, it's assumed to be in octal notation. The digits 8 and 9 are illegal in octal—the digits can range only between 0 and 7.
Because it's octal, an octal number has 8 digits which spans from 0 to 7 inclusive. For the same reason 12 would be an invalid binary number.
You need at least base 9 to have 18 and a normal decimal base for 19.
For your query.....you assign an invalid value to the variable....your assigned value is started with 0(zero) ..which means that you are assigning an octal value to the variable and when you assign a value higher then 7 such as 018 in your case...the value excedes the range of octal variables and hence show an error...so try entering simply 18 so it would take it as an integer rather than an octal variable data type...

How does Java store Negative numbers in an Integer Variable? [duplicate]

This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
How does this snippet of code prints "-511" as the output on the console?
class Test
{
public static void main(String[] args) {
int i = -0777;
System.out.printf("%d",i);
}
}
Is it to do with the way Java stores Negative numbers?
Integer numbers prefixed with 0 are octal numbers. To use decimal numbers remove the 0 prefix:
int i = -777;
Numbers starting with 0 are treated as being in octal by Java. -077 is equivalent to -63, which is what I get when I run your program.
When a number in java code starts with a 0, it interprets it as octal format

Printing binary values in Java

Why the output of this java program is 8 and 9 while we try to print 010 and 011 respectively:
public class Test{
public static void main(String str[]){
System.out.println(011);
}
}
What is the reason?
In Java, the default conversion from integer to string is base 10. If you want a different radix, you have to specify it explicitly. For instance, you can convert an integer value to a binary string using:
Integer.toString(value, 2);
Similarly, you can convert it to an octal value with:
Integer.toString(value, 8);
Unless the value is 0, the string will not have a leading zero. If you want a leading 0, you'll have to prepend it. Alternatively, you can format it with String.format() and specify zero fill and a minimum width for the converted string:
String.format("%1$03o", value); // the "1$ in the format string is optional here
P.S. It's not clear from your question what the exact problem is. It sounds like it's that you are converting from an integer value to a string. However, if the problem is that you're trying to read the string "011" and it is being read as the integer value 9, this is because a leading 0 forces interpretation as an octal value. You'll have to read it as a String value and then explicitly specify the conversion from string to integer to be base 10:
int val = Integer.valueOf(stringValue, 10);
010 is interpretetion of 10 in octal base which is 8. And similarly 011 is interpretetion of 11 in octal base which is 9.
Prepending an integer with 0 makes it interpreted in octal base.
Similarly, you can try printing 0x10, which is a hexadecimal representation, and will give you 16 as output.
- If you want to convert the integer to binary data, use toBinaryString() method.
Eg:
int i = 8;
Integer.toBinaryString(i);
bec. when you start your number with 0 JVM convert the number from decimal to octal that's all ;)
"11 in decimal = 9 in octal"
decimal 0 1 2 3 4 5 6 7 8 9
octal 0 1 2 3 4 5 6 7 10 11

Categories

Resources