byte[] array in Java(size declaration) [duplicate] - java

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

Related

Doubts with Java Primitive Data type (Byte and Int) [duplicate]

This question already has answers here:
Why i am getting type mismatch: cannot convert from int to byte
(2 answers)
Java: why do I receive the error message "Type mismatch: cannot convert int to byte"
(5 answers)
Closed 3 years ago.
When i do the below code, it ask me for casting the value on right side ?
byte myByteTestValue = Byte.MAX_VALUE + 1;
When i do the below code, it didn't ask me for casting the value on right side ?
int myIntTestValue = Integer.MAX_VALUE + 1;
why this is happening, anyone can explain ?
The + 1 is treated as an Integer which is larger than a byte. Cast it and the warning goes away.
byte myByteTestValue = Byte.MAX_VALUE + (byte)1;

Why is java allowing this to compile and what is it interpreting multiple + signs as in this case? [duplicate]

This question already has answers here:
Why does this Java code with "+ +" compile?
(8 answers)
Explanation about a Java statement
(6 answers)
Closed 4 years ago.
Wasn't sure exactly how to word the question, but I noticed something strange while constructing a date. I found that if I construct a date like this
new Date(+ 1)
it compiled just fine, and so did
new Date(+ + + 1)
If I execute the following, the output is 1
public static void main(String[] args) {
int x = 1;
System.out.println(+ + + + x);
}
Can anyone explain what it is that the JVM thinks I am doing?
It's the unary operator (+). You can always add a + to a numeral and that will give you the positive value of the number.
Because you're spacing the tokens out in such a fashion, the lexer is not interpreting anything here as incrementation, so you're adding four unary (+) operations to a value 1.
It's treating it like this:
System.out.println(+ (+ (+ (+ x))));
This is no different than
System.out.println(- (- (- (- x))));

Why explicit cast required when adding int with byte? [duplicate]

This question already has answers here:
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
(3 answers)
Closed 7 years ago.
Doing this is legal in Java
byte = 27 // 27 treated as int; implicit cast to byte
But when assigning a value as a result of expression, Java requires explicit casting
int a = 9;
byte b = 8;
byte c = a + b; // Compile error
What is the reason behind this?
27 is a literal. The compiler knows that it is representable in a byte (from -128 to 127).
a + b is an expression involving variables. Its result may not be representable in a byte

How to negate a number using shift operator [duplicate]

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

Java type mismatch cannot convert from int to byte? [duplicate]

This question already has answers here:
Add bytes with type casting, Java
(7 answers)
Why is the sum of bytes integer? [duplicate]
(1 answer)
Java: why do I receive the error message "Type mismatch: cannot convert int to byte"
(5 answers)
Closed 4 years ago.
I'm wondering why the //1 statements are accepted by the compiler and the //2 statements are not
//1
int k = 99999999;
byte l = (byte)k;
//2
byte b = 1;
int i = 10;
byte z = (byte)i+b; //compiler rejected
Type mismatch: cannot convert from int to byte using ternary operator gave me somewhat of an idea but I don't understand why the compiler can resolve the variable l in //1 as acceptable and not i in //2
You cast to byte just the first number i and not the whole sum. You have to add the brackets:
byte z = (byte) (i+b);
Plus always returns int. So you need to explicitly typecast whole expression to byte.
(byte)(i+b)

Categories