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.
Related
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 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 2 years ago.
Improve this question
I am trying to convert some C code into another language (eg: Java or C#). The problem is understanding a few things about how these arrays are being declared and used...
This is the minimum code that demonstrates my issue:
static void some_function ( )
{
int16_t arr_tmpShort[120 + 40], *ptr0, *ptr1;
int offset = 5;
//assume that "arr_tmpShort" is now filled with some values
ptr0 = arr_tmpShort + 84 - offset;
ptr1 = arr_tmpShort + 85;
}
So I need a second opinion:
This line:
int16_t arr_tmpShort[120 + 40];
is creating an array for holding 160 entries of Shorts. That plus sign is doing nothing special except simple arithmetic, right?
Problem: Now these lines
ptr0 = arr_tmpShort + 84 - offset;
ptr1 = arr_tmpShort + 85;
are strange cos I'm seeing arithmetic on an array.
This is new to me and after some research I need clarification on which of the below is more valid or true...
ptr0 = arr_tmpShort + 84 is equal to Java/C# as arr_tmpShort[84] (a position in array)?
Is it considered as ptr0 =
either (arr_tmpShort[84] - offset); //get [84] Short value and minus it by offset?
or (arr_tmpShort[84 - offset]); //get [84 - offset] Short value from array?
1) Yes, most likely that will be optimized out to 160 by the compiler.
2) In c, you can think of arrays and pointers as being the same thing.
So, when you have code like i[3] this would be equivalent to writing *(i+3). Both of these return the value of the element stored in the 3rd memory location after the start of the array i. More information on pointers can be found here
So ptr0 = arr_tmpShort + 84 - offset is going to be equal to the memory location of arr_tmpShort[84 - offset] which in this case is arr_tmpShort[79].
Later you could also write *ptr0 and if no other modifications are made, it would be equal to arr_tmpShort[79].
C pointer arithmetic: https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm
ptr0 = arr_tmpShort + 84 is equal to Java/C# as arr_tmpShort[84] (a position in array)?
No. It calculates the address needed, but doesn't actually access a value (either read or write).
Is it considered as ptr0 =
or (arr_tmpShort[84 - offset]); //get [84 - offset] Short value from array?
It's closest to this one, but again there's no access of the element. The address of arr_tmpShort[84-offset] is caculated, but no access is done yet.
To access the element, you typically have to derefernce the variable.
ptr0 = arr_tmpShort + 84;
short x = *ptr0; // this retrieves the 84th element, assuming a short
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 6 years ago.
Improve this question
I'm currently learning Java and I stumbled on an exercise I can't finish.
The task is to write a recursive method that takes an array and returns the difference of the greatest and smallest value.
For example {12, 5, 3, 8} should return 5 (8 - 3). It is important to note that it is only allowed to compare values in their right order (result = rightValue - leftValue). For example 12-3 = 9 would not be allowed. Think of it like stock values. You want to find out which time to buy and sell the stocks to make the largest profit.
It was quiet easy to implement this iterative but I have no idea how to do it recursive. Also it is part of the task to solve it by using divide and conquer.
I've used divide and conquer approach here. I believe the trick here is to include middle in both the arrays that we're splitting the main array into.
/* edge cases ignored here */
int findMax(int[] arr, int left, int right){
if(right-left == 1) return (arr[right]-arr[left]);
int middle = left + (right-left)/2;
int max1 = findMax(arr, left, middle);
int max2 = findMax(arr, middle, right);
if(max1 >= 0 && max2 >= 0) return max1+max2;
else return Math.max(max1,max2);
}
Well I don't think recursion is very effective on this. You would probably never do this(other than homework). Something like this would do it:
int findGreatestDifference(Vector<Integer> numbers, int greaterDifference){
if(numbers.size() == 1 ) //return at last element
return greaterDifference;
int newDifference = (numbers.get(0) - numbers.get(1));
if (newDifference > greaterDifference)
greaterDifference = newDifference;
numbers.remove(numbers.size() - 1);
findGreatestDifference(numbers, greaterDifference);
return greaterDifference;
}
first time you call it, pass 0 as the greater difference, and again I don't find this as an effective way to do it. Iteration would be way better for this.
I hope this helps.
Algorithm (this is pretty much a sort task , then the subtraction step is trivial)
1) First sort the arrays (use recursive merge sort for large arrays and recursive insertion for smaller arrays).
Merge sort (https://en.wikipedia.org/wiki/Merge_sort)
Insertion sort (https://en.wikipedia.org/wiki/Insertion_sort)
2) Use the arrays smallest index[0] to get the smallest value & index[array.length-1] to get the largest
3)compute the difference (dont know what you mean by right order?)
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);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two arrays a[] and b[].
int a[]={3,1,1,1,7,4,6,6,3,1};
int b[]=new int[a.length];
Array length in actual problem can change.
The values in the array must be less then value of array length as seen.
The output must be:
b = 3 1 0 0 7 4 6 0 3 1
So basically if there is a sequence of same value in a[] then only 1st of its value must be placed at same index in b[] rest should be zero till the sequence exists.
Answer in java syntax will be helpful.
Thank you in advance
int a[]={3,1,1,1,7,4,6,6,3,1};
int b[]=new int[a.length];
int temp = a[0];
b[0] = temp;
for(int i = 1; i < a.length; i++) {
if(a[i] == temp)
b[i] = 0;
else
b[i] = a[i];
temp = a[i];
}
hint:
1) create hashmap to store processed values
2) iterate over the first array: if current value is stored in map, then fill 0, otherwise store this value in HashMap and copy the value to new array