posted 12 years ago

Hello people,

how to make a signed int to unsigned int, i want to show the int number more than 2147483647 for positiv number. We know that int have 4 Byte = 32 bits that means -(2^16)<number<(2^16)-1, i want to make it (2^32)-1 positiv number.

Any help will be appreciated, thanks.

how to make a signed int to unsigned int, i want to show the int number more than 2147483647 for positiv number. We know that int have 4 Byte = 32 bits that means -(2^16)<number<(2^16)-1, i want to make it (2^32)-1 positiv number.

Any help will be appreciated, thanks.

posted 12 years ago

Java does not have unsigned integers. So as Jeff shows, you can use a larger type (a long, which is 64 bits) to hold values that don't fit into int (which is 32 bits).

benny rusli

Ranch Hand

Posts: 72

posted 12 years ago

Hello people,

can someone explain me, what is the meaning of this row from the code below : "String s = Integer.toHexString( digest[i] & 0xFF );" , what is digest[i] & 0xFF. When to use byte and when to use int ? whether the both is same ? if i print out the byte = 20; is the same if i print out int = 20;. Many thanks for responding my question.

can someone explain me, what is the meaning of this row from the code below : "String s = Integer.toHexString( digest[i] & 0xFF );" , what is digest[i] & 0xFF. When to use byte and when to use int ? whether the both is same ? if i print out the byte = 20; is the same if i print out int = 20;. Many thanks for responding my question.

posted 12 years ago

First of all, digest[i] gets the ith element from an array named digest. 0xFF is a int constant. The "0x" at the beginning means that it is represented in hexadecimal (hex), which is popular for representing binary numbers. The hex value of FF is 255. If you need more details on how this works, you should google for information on number systems.

Finally, the & operator performs a bit-wise AND operation. That is, it takes the corresponding bits in each of the operands. If both bits are 1, then the resulting bit is 1. Otherwise, the resulting bit is 0. Perhaps an example will make this more clear:

I am using a binary representation here for "5 & 3". The result is 1. When we do operations on bits, we typically use hexadecimal instead of decimal to represent numbers. Again, if you want to learn more about this, you should google for more information about how computers represent integral numbers.

A byte is 8-bits whereas an int is 32 bits. So the difference is in the numbers that you can represent. Personally, I don't use byte very often unless I need to manipulate individual bits. Even then, sometimes int is appropriate.

Layne

Originally posted by arifin rusli:

Hello people,

can someone explain me, what is the meaning of this row from the code below : "String s = Integer.toHexString( digest[i] & 0xFF );" , what is digest[i] & 0xFF. When to use byte and when to use int ? whether the both is same ? if i print out the byte = 20; is the same if i print out int = 20;. Many thanks for responding my question.

First of all, digest[i] gets the ith element from an array named digest. 0xFF is a int constant. The "0x" at the beginning means that it is represented in hexadecimal (hex), which is popular for representing binary numbers. The hex value of FF is 255. If you need more details on how this works, you should google for information on number systems.

Finally, the & operator performs a bit-wise AND operation. That is, it takes the corresponding bits in each of the operands. If both bits are 1, then the resulting bit is 1. Otherwise, the resulting bit is 0. Perhaps an example will make this more clear:

I am using a binary representation here for "5 & 3". The result is 1. When we do operations on bits, we typically use hexadecimal instead of decimal to represent numbers. Again, if you want to learn more about this, you should google for more information about how computers represent integral numbers.

A byte is 8-bits whereas an int is 32 bits. So the difference is in the numbers that you can represent. Personally, I don't use byte very often unless I need to manipulate individual bits. Even then, sometimes int is appropriate.

Layne