java negative binary literal [duplicate] - java

This question already has answers here:
How do you specify a byte literal in Java?
(6 answers)
How to specify a constant is a byte or short?
(3 answers)
Java binary literals - Value -128 for byte
(1 answer)
Closed 4 months ago.
So to convert a positive binary number into a negative one I have to invert all of the bits of the number and ​add 1.
`byte x = 0b0111_1111;
System.out.println(x); // 127
x = 0b1111_1111_1111_1111_1111_1111_1000_0001;
System.out.println(x); // -127
I don't understand why I have to expand my number to 32 bit form by adding zeros on the left side before reversing the bits, why exactly to 32 bit form?

Related

Java modulo doesn't work with large numbers [duplicate]

This question already has answers here:
Modulus with doubles in Java
(4 answers)
Taking Modulo of Double NUmber
(3 answers)
Closed 5 years ago.
I am creating a program to calculate Mersenne primes in Java. While doing so, I noticed that the modulo % operator doesn't function properly when dealing with large numbers.
Here is my code:
double a = 2305843009213693951d; //2^61 -1
double b = 2;
double m = a % b; //this should be 1.0
System.out.println(m); //prints 0.0
While one would expect the result to be 1, since the original number isn't even, it actually returns 0.0.
Why is this, and how can I fix it?

Convert decimal number to BigInteger in Java [duplicate]

This question already has answers here:
Datatype to store 20 digit number
(4 answers)
Closed 6 years ago.
I am trying to convert 10000000000000000000000000000000 to BigInteger.
But it is not converting.
My code is
BigInteger number = BigInteger.valueOf(10000000000000000000000000000000);
It is showing The literal 10000000000000000000000000000000 of type int is out of range.
Is there any way to convert this number as I have to use this number as integer in my programme?
The whole point of BigInteger is to support numbers long and int cannot so you can't use those. What you can do is use a String.
BigInteger bi = new BigInteger("10000000000000000000000000000000"):

How to convert a two bytes number in two indivudal numbers with 8 bits? [duplicate]

This question already has answers here:
Convert integer into byte array (Java)
(11 answers)
Closed 8 years ago.
Suppose i have the number 520 that is mapped in two bytes that gives me the number: 1000001000 and i want to convert this number (520) to two other numbers, these numbers should be: 2 and 8 because 00000010 will give me 2 and 00001000 will give me 8. how can i do this with java?
Like this:
int theNumber = 520;
byte oneNumber = (byte)theNumber;
byte otherNumber = (byte)(theNumber >> 8);

Flip all bit values in a byte [duplicate]

This question already has answers here:
Bitwise operator for simply flipping all bits in an integer?
(16 answers)
Closed 8 years ago.
I would like to flip all the bits in a byte. How would I do that?
Ex:
input: 10101000
output: 01010111
Use the ~ bitwise complement operator.
byte flipped = (byte) ~original;

Show integer as binary sequence (java) [duplicate]

This question already has answers here:
Print an integer in binary format in Java
(24 answers)
Closed 8 years ago.
Say I have an integer number: 11728322732
how can i convert it to a string according to its byte representation, i.e 11100011000000001100000111110001 (32 bits \ 4 bytes \ integer)
Thanks
Here it is:
Long.toBinaryString(11728322732L);
Actually, 11728322732 is not an Integer but a Long (because it is greater than Integer.MAX_VALUE). So if you really want to convert this long to a 32-bits int (can't figure why actually), you could do:
Integer.toBinaryString((int)11728322732L);

Categories

Resources