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 1 year ago.
Improve this question
How to make that after the negation operation, i.e. ~ 10 (binary: 1010), the result would not be -11 but 5, 10 = binary 1010 and after negation 0101 or 5.
Thank you for help!
The binary representation of 10 is not 1010, it's 000...0001010 (with total of 32 bits).
Therefore the negation is 111...1110101, which is -11.
If you want to get 5, you need to keep only the least significant 4 bits (and reset all the rest to 0):
int x = 10;
System.out.println (~x & 0xf);
For a more general solution, if you want to negate only the n least significant bits (where n-1 is the index of the highest 1 bit in the input number) and keep all the higher bits 0, you can use Lino's suggestion:
System.out.println (~x & ((Integer.highestOneBit(x) << 1) - 1));
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
My question might be so basic but I'm still asking this. I reffered this post and what I understood in the logical & does bitwise AND
0101 & 0011 = 0001
Is there any usecases where we can apply & on decimal numbers?
For example , if you consider logical ^ (XOR) , it is useful to find unique number in an array, where all the other elements present twice.
Eg: if you have an array [1,1,2,2,3,3,4,4,6,6,7]
and you need to get 7
int[] x = new int[]{1,1,2,2,3,3,4,4,6,6,7};
int y = x[0];
for (int i = 1; i < x.length; i++) {
y ^= x[i];
}
System.out.println(y);
This code will give the result quickly,
Similarly is there any usage of & in such usecases?
There is no such thing as "apply & on decimal numbers". Numeric values are stored in binary in the computer. The default printing of numeric values is in decimal format, but that's just intended to make them human readable.
As for example for using & (bit-wise AND) on ints, when you have an int bit-mask (in which each bit represents something), you can use bit-wise AND to determine the value of any specific bit.
For example:
int mask = 13;
if (mask & 2 > 0) { // this tests if the 2nd lowest bit of mask is 1
}
if (mask & 4 > 0) { // this tests if the 3nd lowest bit of mask is 1
}
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
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I recently stumbled across this issue whilst programing. I wonder how this is possible, as modulo returns the remainder of the euclidean division a / b. Doesn't this mean that it would have to return 3 since returning 0 would mean that you have to perform a division by 0, which is impossible? Also, how is this handled in other programming languages? I know that modulo has very varying implementations throughout different languages, but is this a variable aspect or is it set by some mathematical statement?
Doesn't this mean that it would have to return 3 since returning 0 would mean that you have to perform a division by 0
No. You're not dividing 3 by 0, you're dividing 0 by 3, which of course yields a modulus of 0.
If a % b is the remainder of a / b, then 0 % 3 must be the remainder of 0 / 3. Since 0 / 3 = 0, there is no remainder. There is no division by zero.
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
The expression is: n mod m = x
I want to know the value of n given m and x
Is possible to get this value ? Is there is a java function to get that number?
It isn't possible to get this value. There are in fact multiple possibilities for a given m and x. For example, take n mod 3 = 1. We know that m is 3 and x is 1, but just knowing that, we don't know whether n is 4 or 7 or 10 or 13 or any other number that is one more than a multiple of three.
I don't think that can be done - look at a few examples
10 mod 9 = 1
19 mod 9 = 1
n could be 10, 19, 28 etc...
or
9 mod 8 = 1
17 mod 8 = 1
n could be 9, 17, 25 etc...
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 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321