This question already has answers here:
Change sign using bitwise operators
(3 answers)
Closed 7 years ago.
I need to negate a number using shift operator.
Example:
If number = 5 then negation of 5 should be -5
If number =-5 then negation of -5 should be 5.
Not with the shiftoperator but there is other bitwise operator using them you can do this
int i = 10;
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
result
-10
10
-10
10
BTW it is example of Two's complement and used for binary signed number representations
Related
This question already has answers here:
The power operator in Java?
(1 answer)
What does the ^ operator do in Java?
(19 answers)
Closed 1 year ago.
I have declared 3 different types of byte arrays(of different sizes). See the comment next to each as I am not able to understand how the length is computed by the compiler?
byte[] byteField0 = new byte[2^3];
System.out.println("bitField0 " + byteField0.length); // Gives 1 byte instead of 8?
byte[] byteField2 = new byte[2^5];
System.out.println("byteField2 " + byteField2.length); // Gives 7 bytes instead of 32?
byte[] byteField3 = new byte[8];
System.out.println("bitField3: " + byteField3.length); // Gives 8 bytes as expected
This has nothing to do with array size. Print those numbers individually, or more importantly, as binary.
^ is XOR bitwise operator, not a replacement for Math.pow
Or as mentioned in comments, powers of two can be accomplished with a different bitwise operator, the left shift <<, which would be computationally faster than Math.pow
This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 6 years ago.
I cannot understand to why this syntax does not generate any kind of compile time or run time errors ?
int i=2;
switch(i ^ 3){ ---- > this part
case 8: System.out.print("Eight"); break;
default: System.out.print("Default");
}
It prints Default, so what does this ( i ^ 3 ) do in the switch condition ?
i ^ 3 is i XOR 3 (2 XOR 3), which is 1 (10 XOR 11 is 1). It's not a power operator, so it doesn't return 8. Therefore the default section of the switch statement is reached.
i is equal to 2.
2^3 = 1. (XOR operation).
10 //2
11 // 3 (XOR)
--
01 //1
So, it sets the value of 1 for the switch condition.
^ is Bitwise XOR where i ^ 3 generate integer result.
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 7 years ago.
I have a code like this, but I don't know why result variable have false value after execution the code
int x = 234;
boolean result = (x<0250);
and also why the following code doesn't work properly?
System.out.println(0250);
it prints 168 !! why?!
Integer literals starting with 0 are octal (base 8) not decimal (base 10).
Your options are
hexadecimal = 0x0C;
decimal = 12;
octal = 014;
binary = 0b1100;
A number that looks like 0x followed by an integer (using the digits 0-9 and A-F) is a hexadecimal (base 16) number.
A number that looks like 0 followed by an integer (using the digits 0-7) is an octal (base 8) number.
This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Adding a leading zero to a large string in Java
(2 answers)
Closed 8 years ago.
I'm using an int variable:
month = dp.getMonth() + 1;
currently getting an output of "2" and when I do the following:
if (month<10){
month = '0'+month;
};
I get: 50.
Your problem is that your '0' char is being coerced to an integer. Since '0' has an ASCII value of 48, you're getting 48 + 2 = 50.
Note that what you're trying to do won't work - you can't add a leading 0 to month, as month is a number. A leading zero only makes sense in a string representation of a number.
As explained in this answer, here's how to produce a zero-padded number:
String.format("%02d", month);
This question already has answers here:
Left padding a String with Zeros [duplicate]
(20 answers)
Closed 8 years ago.
How to add zero as prefix if value less than equals to 9, i am using below way of achieving this:
int countZero = 0;
if(countVat <= 9)
{
countVat = countZero + countVat;
Log.d("countVat:", String.valueOf(countVat));
}
but this not works for me, still getting single digit if countVat value less than equals to 9.
Use String.format
String.format("%02d", num);