Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am doing some experiments on my own about quantization processes etc.
I try to implement a binarization process which makes a "binary string" which will get processed by xor afterwards and some other stuff.
Anyhow the binarization is the following, where d and u are some numbers that will get compared:
String b = "";
for (int i = 0; i < u.length; u++) {
if(d[i] < u[i]) {
b[i] += '0';
} else {
b[i] += '1';
}
}
Currently like described I have a string where each character is 0 or 1.
Using a BigInteger gives me an Object where I can XOR two values against each other:
BigInteger bi = new BigInteger(b, 2);
(...)
BigInteger result = bi.xor(other_bi);
Is there another way to achieve what I want to do? I didn't find anything but maybe there is one I have not found?
The BitSet class is more appropriate for representing a sequence of bits. To set a bit you would use the BitSet.set method.
Taking your example as a BitSet.
BitSet bs = new BitSet(u.length);
for (int i = 0; i < u.length; u++)
if(d[i] >= u[i])
bs.set(i);
BitSet bs2 = ...
bs2.xor(bs); // changes and reuses bs2.
int bitsSet = bs2.cardinality();
int firstSetBit = bs2.nextSetBit(0);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
How to convert the
number(random integer within 0 – 255) to binary and store the bit into an 8 bit array.(java)
You may look at BitSet class, which implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined:
BitSet myByte = new BitSet(8);
To convert the byte value to the BitSet, there are methods BitSet.valueOf and BitSet::toByteArray:
byte b = 111;
BitSet bits = BitSet.valueOf(new byte[]{b});
byte fromBits = bits.toByteArray()[0];
You can use Bit Masking to convert an int to binary.
public static int[] getBits(int number) {
assert (0 <= number && number <= 255);
int[] bits = new int[8];
for (int i = 7; i >=0; i--) {
int mask = 1 << i;
bits[7-i] = (number & mask) != 0 ? 1 : 0;
}
return bits;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k is evaluated k=9 and then that becomes k*(9-8) giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
You have a multiplication with
left side: k
right side: (++k-8)
As you correctly stated, braces have precedence. However, your program still runs "from left to right". So first the left side is evaluated, which is k = 8. Afterwards the right side is evaluated, which is (++k-8) = 1.
Now we have determined both sides and can multiply them together: 8*1 = 8.
this is the class file your code compiled:
int k = 8;
byte var10000 = k;
int k = k + 1;
int p = var10000 * (k - 8);
System.out.println(p);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Short question. I am quite new to doing bit/bytewise operations in Java, but I noticed something strange.
The code below gives as output:
2
0
code:
String[] nodes = new String[3];
boolean in;
int index = 0;
int hash = "hi".hashCode();
in = (nodes[(index = nodes.length - 1) & hash]) != null;
System.out.println(index);
index = (nodes.length - 1) & hash;
System.out.println(index);
Why is it, that index has a different value even though the operation to assign a value to index is identical in the 5th and 7th line of code?
Like I said, I'm new to bitwise/bytewise operations so I'm probably missing some background information.
(index = nodes.length - 1) & hash
assigns nodes.length - 1 to index, then bitwise-ands it with hash.
index = (nodes.length - 1) & hash
bitwise-ands nodes.length - 1 with hash, then assigns the result to index.
Not the same thing.
Your first assigment to index happens in the parenthesis here:
in = (nodes[(index = nodes.length - 1) & hash]) != null;
Which is
index = nodes.length - 1
and has a value of "two". Your second assignment (index = (nodes.length - 1) & hash;) differs; you added a & hash which presumably has the two bit as 0 thus when you perform the bitwise & it becomes 0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Lets say I have 3 Integers:
int a, b, c;
b = 25;
c = 10;
Now I want a to be either 25 or 10 but by random not something like:
a = b;
I want something like in if statement:
a = b || c;
How can I achieve it?
if(Math.random() < 0.5)
a = 25;
else
a = 10;
Math.random() returns a random number from 0 to 1, so if you want a 50% chance of something being true, just check if it's less than (or greater than) 0.5.
one way is to do by taking the millis of time like:
if(System.currentTimeMillis() % 2 == 0){
a=b;
} else{
a=c;
}
#immibis' answer is the simplest way to achieve this.
For testability, I would strongly advise that you use an explicit Random instance, rather than using Math.random():
static int pickRandomValue(Random r, int b, int c) {
return r.nextInt(2) == 1 ? b : c;
}
This allows you to inject a mock Random instance, allowing you to fix the behaviour when you need to test specific behaviour. Non-deterministic tests are a pain, and should be avoided.
Try the below code:
Random rand = new Random();
int myRandom = rand.nextInt(2); // will be 0 or 1
if (myRandom == 0) {
a=b;
} else {
a=c;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to squeeze every but out of my Java Othello program and have a point where I need to count the number of instances a given number appears. For instance array[]{1,1,2,1,0,1} would count(1) returns 4. Below was an attempt I made at speed by counting all numbers but this was slower:
public void count(int color) {
byte count[] = new byte[3];
for (byte i = 0; i < 64; i++)
++count[state[i]];
return count[color];
}
So far this is the most efficient code I have tested:
public void count(int color) {
byte count = 0;
for (byte i = 0; i < 64; i++)
if (this.get(i) == color)
count++;
return count;
}
Does anyone think they could squeeze some more speed out of this? I only need the count of the number specified, nothing more.
Use int, not byte - internally, Java converts the byte to an int, then increments it, then converts it back to a byte; using an int obviates the need for the type conversions.
You can also try using an AtomicInteger, its getAndIncrement method may be faster than the ++ operator.
You can also unroll your loop; this will reduce the number of times that i < 64 is evaluated. Try using an AtomicInteger for i, and use getAndIncrement instead of ++
for(int i = 0; i < 64;) {
if(this.get(i++) == color) ...
if(this.get(i++) == color) ...
if(this.get(i++) == color) ...
if(this.get(i++) == color) ...
}
Changing the for loop to a do-while loop may be slightly faster - the for loop has a conditional jump and an unconditional jump, but a do-while loop only has a conditional jump.
You can do this in parallel (thread1 counts elements 0-15, thread2 counts elements 16-31, etc), but the cost of creating the threads probably isn't worth it.
try making count int instead of byte some architectures have trouble handling single bytes so a byte is smaller in memory but problematic to make calculations.
1) this.get(i) in version 2 seems suspicious, if we are dealing with array, array[i] is supposed to be more efficient.
2) I would replace
byte count = 0;
for (byte i = 0; i < 64; i++)
...
with
int count = 0;
for (int i = 0; i < 64; i++)
...
otherwise Java will need to promote byte operands to int to do arithmetic and then truncate results to byte.
3) Use http://code.google.com/p/caliper/ to get good benchmarks
You can use collections for this purpose. But your array should be type of Integer because collection don't support primitive types.
public void count(int color) {
List<Integer> asList = Arrays.asList(your_Array);
return Collections.frequency(asList,color);
}