Understanding Pointer Arithmetic of Array Declared in C [closed] - java

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

Related

Bitwise operations in Java give different value with similar code? [closed]

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.

Get a number in an interval [closed]

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
I would like to get a number between 5 and 250 from two variables according to the following pseudo-code:
arg[1-19] * something = [5 - 250]
arg will contain values (whole numbers) in the interval [1 - 19]; something could be any whole number.
The problem is that I obtain the same result using different combinations of arg and something:
arg: 1, something: 10 => result 10
arg: 2, something: 5 => result 10
How can I make sure that the result is different for any different pair of arg and something?
Is there a mathematical function or a function in java to do this?
I don't think you have a problem with programming. Instead, I think you should go one step back and try to understand your problem at mathematical level. Consider the equation
(1) a * b = C
for any (arbitrarily chosen) C, with a, b and C being whole numbers.
Unless a and b are both prime, there will at least be one pair of other a, b which also satisfy the equation. The proof is very simple if you think about it for a minute:
Suppose that a is not prime (the proof would be the same for b being not prime). Then there must be at least two whole numbers p and a' (p not being one by definition) so that
(2) a = p * a'
Now define that
(3) b' = p * b
Using equations (2) and (3), you can re-arrange equation (1) like that:
(4) C = a * b = (p * a') * b = a' * (p * b) = a' * b'
q.e.d
Here, since p != 1 (by definition - see above), a != a' and b != b'.
This means that you must have both factors a, b prime if you want to guarantee that every different pair (a, b) will give a different result a * b.
You did not tell us what you are actually trying to achieve, but probably there is a better solution than multiplying two whole numbers.
For example, if we knew that your something is always less than ten (i.e. is 0, 1, ..., 9), you could do (arg * 10) + something (this is only an example to show the principle; it would give values between 10 and 199 considering the restriction for arg you have mentioned, so it is actually not a solution to your problem; I have chosen that numbers because our decimal system basically works that way :-) and thus you already know about it).
And finally, so trivial that it possibly doesn't come to mind, remember that a * b = b * a (multiplication is commutative at least for our whole, real and irrational numbers and some more which I don't dare to mention). Applied to your problem: arg * something = something * arg. Of course, this is still true even if arg and something are both prime. It is inevitable that you get the same result if you exchange the values of arg and something. This might or might not be acceptable for whatever you are trying to achieve.
If something can take more than 12 values, this is not possible, as your input space will have a size of at least 19 * 13 = 247 whereas your output space has a size of 245.
If something takes less than 12 values, you can simply do: 19 *$something + arg + 4 assuming you map the values of something to the interval [0,n]
If your goal is to given an "arg" number you return a "something" number such that the result of arg*something is insiade a given interval, try this :
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(findNumber(2, 5, 250));
}
public static int findNumber(int arg, int min, int max) {
Random rand = new Random();
int randMin;
int randMax;
if(min%arg == 0){
randMin = min/arg;
} else {
randMin = (min / arg) + 1;
}
randMax = max/arg;
int result = rand.nextInt(randMax+1-randMin);
result = result + randMin;
return result;
}

How to add two or more numbers and display the sum (Netbeans) [closed]

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
So i'm a beginner and I am trying to figure out how to add more than 2 numbers and display it through the output of netbeans. Its really confusing for me since i'm still new to programming.
First, you need to define variable.
what is variable?
A variable provides us with named storage that our programs can
manipulate.
There are different type variable in Java like int, double, char, String, and so on.
What do I mean by type variable?
determines the size and layout of the variable's memory
he range of values that can be stored within that memory
the set of operations that can be applied to the variable
How to define Variable in Java
data type variable [ = value][, variable [= value] ...] ;
For example , you can define two variable as int type as follows
int firstNumber = 1;
int secondNumber = 2;
Note if you like you can define more than two variables by following up the rules.
you can do Arithmetic Operators on your variables
Operator Description
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Here you want to add so you need to use + operator.
read this which is my source : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
At the end, you should define a variable that carries the summation of two variables like
int sum = number1 + number2;
and print the value on your console by using System.out.print/ln
System.out.print("the summation of number one and number two is " + sum);
Source:
http://www.homeandlearn.co.uk/java/java_int_variables.html
http://www.tutorialspoint.com/java/java_variable_types.htm
int a = 1;
int b = 2;
int c = 3;
int d = a + b + c;
System.out.println(d);
If you want to add up the numbers in an array or collection:
int[] numbers = { 1, 2, 3 };
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println(sum);

Recursive Equations with Time Complexity [closed]

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 really struggling with this recursive question. Can anyone help me solve the recurrence T(n) = 5T (n/5)+5 with the base condition T(1) = 0 via closed-form formula? It is given that n = 5^m with the integer m = log5 n.
It will be sufficient to compute T(5n) for n >= 0. For all other values of x, T(x) will equal T(y) where y is the largest power of 5 smaller than x, since the calculations are the same. (I'm assuming that when you write n/5 you mean integer division, i.e. floor(n/5).)
Then:
T(50) = 0
T(51) = 5 * 0 + 5 = 5
T(52) = 5 * 5 + 5 = 52 + 51
T(53) = 5 * (5 * 5 + 5) + 5 = 53 + 52 + 51
... which leads to:
T(5n) = 5n + 5n-1 + ... + 52 + 51
which, using a high-school algebra formula (sum of a geometric series), is
T(5n) = (5n+1 - 5) / 4
If you're thinking about time complexity, notice that T(x) will always be less than or equal to 5x / 4. And since we don't worry about constant factors when expressing things in O-notation, this essentially means T(x) = O(x).
A non constructive way to solve this: looking a bit at the formula one guesses that T(5m) = (5m+1-5)/4. This can be shown by induction:
it is correct for m=0: T(1) = 0
assuming it is correct for m we show it for m+1: T(5m+1) = T(5*5m) = 5T(5m)+5 = 5*((5m+1-5)/4)+5 = (5m+2-25)/4+5 = (5m+2-5)/4.
Therefore it is correct for all m.

Use java to perform binary shifts [closed]

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 to take a binary data in java and perform shifts on it.
For e.g. 11110000 when I right shift it by 2 , i.e. 11110000>>00111100
The problem is that, I don't know how to store such a data in Java, as the byte type converts it to decimal format. I need to use the bit at position 6 and XOR it with some other value.
I just need to know how I can achieve the ability to store binary data and perform the required shifts.
If you're using an integer it'll display it in a number format, you can work around it using Integer.toBinaryString(yourByte)
Second, if you're using an integer to store a binary number in the format of 1... (the leftmost bit is 1) then using the operation >> will shift the number right but enter a "new" 1 into the leftmost bit. What you want to do in such case is actually use >>> which prevents that.
If you're using an int to store your byte, and you want to start doing all kind of bit manipulations than you'll have to use a mask, for example, switching between the 3rd and the 5th bits:
int number = 7; //just an example, here binary rep: 00111 => requested output will be 10011
System.out.println("number = " + Integer.toBinaryString(number));
int mask3Bit = 4;//binary rep: 0100
System.out.println("mask3Bit = " + Integer.toBinaryString(mask3Bit));
int mask5Bit = 16; //binary rep: 10000
System.out.println("mask5Bit = " + Integer.toBinaryString(mask5Bit));
// now we'll create a mask that has all the bits on except the 3rd and 5th bit:
int oppositeMask = -1;
oppositeMask ^= mask3Bit;
oppositeMask ^= mask5Bit;
System.out.println("oppositeMask = " + Integer.toBinaryString(oppositeMask));
//check if the 3rd bit is on:
mask3Bit = number & mask3Bit;
//shift twice to the right
mask3Bit <<= 2;
System.out.println("mask3Bit = " + Integer.toBinaryString(mask3Bit));
//now do the same with the 5th bit
//check if the 5th bit is on:
mask5Bit = number & mask5Bit;
//shift twice to the right
mask5Bit >>= 2;
System.out.println("mask5Bit = " + Integer.toBinaryString(mask5Bit));
//now we'll turn off the 3rd and 5th bits in the original number
number &= oppositeMask;
System.out.println("number = " + Integer.toBinaryString(number));
//and use the masks to switch the bits
number |= mask3Bit;
number |= mask5Bit;
//let's check it out now:
System.out.println("new number = " + Integer.toBinaryString(number));
OUTPUT:
number = 111
mask3Bit = 100
mask5Bit = 10000
oppositeMask = 11111111111111111111111111101011
mask3Bit = 10000
mask5Bit = 0 //here it's zero cause the original number has zero in the 5th bit and we used &. If the bit was on we would have gotten '100'
number = 11
new number = 10011

Categories