Guys if the int c=10001; which is a binary value.If i want to process it like multiplying it by 10 how to do that?
Treating
int c = 10001;
as a binary number is really bizarre. It would be better to instead declare it as a String
String binaryString = "10001";
and then looping through each character to perform whatever base conversion algorithm you want.
The multiplication of a binary with an integer:
For your example with c=10001 (base 2) * 10 (base 10) this means (10 = 2^3+2^1)
int c=10001
int result=c*1000+c*10 //left-shift 3 + left-shift 1
But this is really not a good way to handle this kind of task... Moreover it think it's a bad idea to save an binary value into an int. I think it would be better to convert the binary into an integer before using it.
You can specify it as int c = 0x11
(consider 10001 is 0001 0001, which is 11 in hex)
public static void main(String[] args) throws Exception {
int c = 0x11; // 10001
int d = 10; // ten decimal
int d = 0x2; // ten binary 0010 - see table below
System.out.println(c);
System.out.println(c*d);
System.out.println(c*e);
}
binary-decimal conversion
an "int" is neither binary, hex or decimal, it's just a place to store a number. Variables themselves don't have a specific hex/dec/binary representation until you print them.
When you type the number into your code it has a base, but after it uses the base to process what you typed, the base is thrown away and the int just stores a number.
So the answer to your question is c * 10 (assuming you meant 10 dec)
If I understand you correctly you want to do this: Integer.parseInt("10001", 2)
, which will give you 17.
Integer.toString
also accepts radix as second argument.
Doc: Integer.parseInt(String s, int radix)