Usage of Logical & in java [closed] - java

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
}

Related

Positive negation in Java [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 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));

How do I split an indice of an array into two different pices, I.E to validate that neither number is odd/even? [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 2 years ago.
Improve this question
I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.
If you assume that you have only two-digit numbers, you can go with it this way:
int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
int second = num % 10;
int first = (num - second) / 10;
System.out.println("first: " + first + " second: " + second);
}
You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.
Now you can process those numbers further.
Your questions have nothing to do with arrays. Let's reword them:
How do I test if a number is even?
You can google this and easily find the answer. Hint: use the modulus operator (%).
How do I test if the second digit is not less than double the first digit
Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).
Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.

How do I display all numbers that end with the number 3 with a for loop in java? [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 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}

Java for every odd multiply of 90 [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 need to create a program which prints something for every odd multiply of 90 so for example
90 - text
180 - nothing happens
270 - text
360 - nothing happens
etc.
Thanks a lot!
A simpler solution is to print the multiples of 180.
for(int i = 180; i < max; i += 180)
System.out.println(i);
I assume this is homework, but there is no point copying someone's answer unless you can explain it. This is for you education purposes. (And your marker will be able to pick up code you probably didn't write yourself)
Your basic structure here should be pretty simple - just use a multiplier and an if statement with a modulus operator.
For example (pseudo-code - check a tutorial if you can't implement this idea):
int multiplier=1;
int maxMultiplier = 10;
int value = 0;
while (multiplier < maxMultiplier) {
value = 90 * multiplier;
if (multiplier % 2 == 0) {
// print something;
}
multiplier++;
}

Find the reverse of a 4 digit integer [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 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

Categories

Resources