Problems with long
Hi everybody
I am new at this forum so first of all sorry if I do something wrong. In addition, my english is not very good.
This is my problem. I am working with an array of long values like this:
long data[]
I have to create a function to set a bit value for this array. This is the function I have created:
finalpublicvoid setBit(int indexBit,boolean valueBit){
// Select the long to change and convert it to a String of bits
int nLongToSet = indexBit / Long.SIZE;
long longToSet = data[nLongToSet];
String strToSet = Long.toBinaryString(longToSet);
// Add 0-s on the left of the String in order to be a String of 64 bits
int numberZerosRequired = Long.SIZE - strToSet.length();
if (numberZerosRequired > 0)
{
String strConcat ="";
for (int i=0; i<numberZerosRequired; i++)
{
strConcat = strConcat +"0";
}
strToSet = strConcat + strToSet;
}
// Select the bit to change
int bitToSet = indexBit % Long.SIZE;
String strResult ="";
// Create the new String
for (int i=0; i><strToSet.length(); i++)
{
if (i == bitToSet)
{
if (valueBit)
{
strResult = strResult +"1";
}
else
{
strResult = strResult +"0";
}
}
else
{
strResult = strResult + strToSet.charAt(i);
}
}
// If the first bit is '1', it is required to change the sign of the long value
long longResult;
if (strResult.startsWith("1"))
{
longResult = Long.parseLong(strResult.substring(1, 63));
longResult = - longResult;
}
else
{
longResult = Long.parseLong(strResult, 2);
}
// Set the new value for data
data[nLongToSet] = longResult;
}
This function works for all the cases except one: if the long to change is "00000...00" with 64 bits and it is required to change the first bit: "10000...00", then the function does not change the value of data. This is because the first bit indicates the sign of the long number and if the number is 0 this bit has no sense.
Is there any solution for this problem? Thanks in advance.>
[3797 byte] By [
astu18a] at [2007-11-27 11:41:37]

What's with all the string parsing?
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
~
@Op. You should explain what you want the method to do, and also provide some examples. What is valid input? What do you expect as result? What happens? What fails?
Kaj
kajbja at 2007-7-29 17:39:10 >

Thanks.
OK sorry. I will give you an example. Imagine this inizialitation for data[].
long[] data = new long[4];
for (int i = 0; i < data.length; i++) {
data[i]=0L;
}
The data long array can be viewed as a 256-bit String with all of them set to '0'.
Next, we call the function setBit(255, true). The expected result will be that the last bit will changes its value to 1. So if we see the data a 256-bit String then it will be something like this:
"00000000000000000000000........000001"
In this case the implemented function works nice. Now imagine we call the function setBit(0, true). In this case the new bit String will be something like this:
"10000000000000000000000........000001"
However in this case the function does not work and we obtain the following bit String:
"00000000000000000000000........000001"
So there are no changes. This is because if we try to parse a 64-bit String to long, the first bit represents the sign (1-negative, 0-positive). If the first bit of the 64-bit String is '1' but the rest of the String is '0', then this sign has no sense and the new long value will be '0' and not '-0'.
I can try to explain it better if you do not understand it at all. Thanks for your help.
I have updated the function setBit because it had a small error
................................
// If the first bit is '1', it is required to change the sign of the long value
long longResult;
if (strResult.startsWith("1"))
{
longResult = Long.parseLong(strResult.substring(1, 64), 2);
longResult = - longResult;
}
else
{
longResult = Long.parseLong(strResult, 2);
}
.............................
Why convert to String?
Why not just operate on the long with bit manipulation?
dwga at 2007-7-29 17:39:10 >

Thanks.
How can I do that?
> OK sorry. I will give you an example. Imagine this
> inizialitation for data[].
Note that the initialization loop is unnecessary in that example; all elements are initialized to 0L by default.
> Next, we call the function setBit(255, true). The
> expected result will be that the last bit will
> changes its value to 1. So if we see the data a
> 256-bit String then it will be something like this:
>
> "00000000000000000000000........000001"
I would expect bit 255 to be the *most* significant bit, but that's not a show-stopper.
> So there are no changes. This is because if we try to
> parse a 64-bit String to long, the first bit
> represents the sign (1-negative, 0-positive).
Don't parse! Just use the appropriate bitwise operator, as I mentioned before.
~
> How can I do that?
Reply #1.
~
OK thanks a lot for your help. I will investigate about BItwise and Bit Shift operators because I have never used them.
Once again, thank your for your help.
Why not use java.util.BitSet?
matfud
Quick: what does this output?
public class LongExample {
public static void main(String[] args) {
System.out.println(12345 + 5432l);
}
}
> Quick: what does this output?
1 d0n't kn0w, what? :o)
~
Im confused. A Long has 8 bytes -> 64 bits.
Whats the 255th bit of a Long?
> > Quick: what does this output?
>
> 1 d0n't kn0w, what? :o)
Dance for me, Groovy monkey!
> Im confused. A Long has 8 bytes -> 64 bits.
> Whats the 255th bit of a Long?
In an array of four long values, the 255th bit would be either the last bit of element[3] or the first bit of element[0], depending on how you choose to look at it. As the OP described, it's the former.
~
> Dance for me, Groovy monkey!
I do the cha-cha like a sssy girl.
I like-a do da cha-cha.
~
That goodness the cuss filter is in place!
Your editing trumps my comment. At least Sun understood that code needs no filter:
int motherfucker = -1;
> That goodness the cuss filter is in place!
Heaven forbid we bandy the word ***** about without finding some reasonable unicode approximation.
~
> Your editing trumps my comment. At least Sun
> understood that code needs no filter:
My eyes! The filter, it does nothing!
~
> Heaven forbid we bandy the word ***** about without
> finding some reasonable unicode approximation.
To that end, I just use HTML entities codes:
http://www.w3schools.com/tags/ref_ascii.asp
for example, "sissy", although they don't survive the roundtrip in replies.