What is an alternative to divide by 2 [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 8 years ago.
Improve this question
this is a question which needs to be answer for a job interview, I only know how to do the following:
int x = y/2;
is there any alternatives ?

Shift right by 1 bit:
int x = y >> 1;

Just to let the interviewer know that you're up for the fun:
int x = 0;
for(int i = 0; i < y; i += 2)
{
x++;
}
Of course, you'll need to do some additional stuff for a negative number, but you get the drill ;)

a << 1 is the same as a * 2. And a >> 1 is the same as a/2

Well as Drakosha said shifting right by 1 :
int x = y >> 1;
or
multiply by 0.5:
int x = (int)(y * 0.5);
or
subtract by the value multiplied by 0.5:
int x = (int)(y - (y * 0.5));

If you don't want to use shifting of bits you could even keep on subtracting the number by 2 till you get the remainder less than 2. Keep a count of how many times you subtracted. Plain maths.

As said, you also could shift the number bitwise. The operator is >> or <<.
Look this example, should make everything clear:
int x = 16;
x = x >> 1;
System.out.println(x); // prints 8

Related

java program to loop and output 2,5,7,10,12 [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 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

Read n, Find the value of M where M = 2 * 4 * 6 * … * ≤ n [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 3 years ago.
Improve this question
n is any integer value, given by the user. M is Multiply of all even numbers in 1..n:
M = 2 * 4 * 6 * … * ≤ n
Example :
int n = 9
output
int output = 2*4*6*8; // 384
My code:
Scanner in = new Scanner(System.in)
int n=inut.nextInt();
for(....)
In short you need to multiply all even numbers between 1 and n.
For this you can use a for-loop and if-statement. for-loop will give you all numbers between 1..n, and if-statement will reject odd numbers, leaving only even.
Last path would be to multiply all values.
int n = 9;// input;
int result = 1; // because you are multiplying, initial result must be 1
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) { // ignore all odd numbers
result *= i; // multiply result with next even value
}
}
System.out.println(result); // print the result: 384
You can look at this like an assembly line. At the start someone is generating numbers from 1 to n. Then someone called 'the filter' rejects (push to trash) all odd numbers, at the end of the line someone called 'the aggregator' multiplies all values into an result.
With Java 8 and streams this can be represented by:
int result = IntStream.range(1,n)// generate numbers from 1 to n
.filter(value->value%2==0) // reject all odd numbers
.reduce(1, (a,b)-> a*b); // multiple all values, with 1 as initial result
System.out.println(result);

x = (x = 1) + (x = 2) * (++x) * (x++) - Why is the output of this expression 19? [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 5 years ago.
Improve this question
I executed the code and output was 19, but I don't understand why.
public static void main(String[] args)
{
int x = 0;
x = (x = 1) + (x = 2) * (++x) * (x++);
System.out.println(x);
}
You evaluate the operands from left to right, and then evaluate the multiplication operators before the addition operator:
x = (x = 1) + (x = 2) * (++x) * (x++);
1 + (2 * 3 * 3 ) = 19
assignment pre post
operator increment increment
returns the returns the returns the
assigned value incremented value before
value it was incremented
its evaluated like this -
1+2*3*3
(x=1) - first x is set t 1
(x=2) - then x is set to 2
(++x) - then x is incremented to 3; pre-increment and affects the equation in this case
(x++) - the last was post increment; no effect on the equations
as per my knowledge:
1 + 2*3*3 = 19

The Nine Tails game [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 7 years ago.
Improve this question
Nine coins are placed in a three-by-three matrix with
some face up and some face down. A legal move is to take any coin that is face up and
reverse it, together with the coins adjacent to it (this does not include coins that are diagonally
adjacent). The task is to find the minimum number of moves that lead to all coins
being face down.
The problem can be reduced to the shortest path problem and solved using BST.
We find all the possible combinations of the 9 coins, and create an UnweightedGraph. Each state (or combination) of the nine coins represents a node in the graph. We assign an edge from node v to u if there is a legal move from u to v.
Here's the algorithm to find all the 512 possible combinations
for (int u = 0; u < 512; u++) {
char[] node = getNode(u);
/* ..... */
}
public static char[] getNode(int index) {
char[] result = new char[9];
for (int i = 0; i < 9; i++) {
int digit = index % 2;
if (digit == 0)
result[8 - i] = 'H';
else
result[8 - i] = 'T';
index = index / 2;
}
return result;
}
How is this algorithm working?
I've only embedded the part I can't understand. If you want I can embed the whole nineTailsProblem.
The logic behind this is that when iterating u=[0, 512), u's binary representation gives you all possible 9-bit combinations of 1s and 0s.
Example with u=[0, 8):
000
001
010
011
100
101
110
111
getNode just converts these u's to char[], representing bit value 0 with H (head) and 1 with T (tail).
This gives you the value of least significant bit (LSB):
int digit = index % 2;
and division by 2 shifts the bits to the right by one, so that in the next iteration, you'll get the 2nd, then 3rd, 4th bit and so on:
index = index / 2;

finding maximum value from a given bits [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 8 years ago.
Improve this question
// a program tells the maximum value we can store in (unsigned integer) a given bits .( 256 for 8 bits)
int counter=0;
int last= 0b11111111;
for(int first=0b00000000;first<=last;counter++)
{
first=first + 1;//adding 1(binary addition)
}
System.out.println("for "+ variable "bits u can store "+counter values");
//variable here 8.
//(1.how to get it from user? 2.how to convert it into binary 0b00000000?)
//how to do this without 0b ,actually in previous version of java
//a program in which if you give 8 bit(in case of unsigned) then it give u maximum values u can store in it, not by using ((2*n)-1).
//code is not only for java 8
// sorry i do not have java 8 i hope the above code will compile without error
thank you in advance
Try this:
long result = 1 << numBits;
If numBits is greater than the size of long, use a double instead (and cast the "1" and "numBits" to double).
how about this
int last = 0;
for(int i = 0; i < bitNum; i++){
last = (last << 1) + 1
}
Scanner s = new Scanner(System.in);
int variable = s.nextInt();
int counter = 0;
long last = (1 << variable) - 1;// = 0b'111....111
for(int first = 0; i <= last; counter++){
first = first + 1;
}
System.out.println("for "+ variable +"bits u can store " + counter + "values");
this code is same with your code, but this code dont use 0b.

Categories