• Log in or sign up for an account here: https://www.sythe.org/login

Coding issue

  • Thread starter Thread starter Darkgroove
  • Start date Start date
D

Darkgroove

Guest
Ok I need to code a program that uses arithmetic operations to reverse an integer input by the user and display it to them.

Eg. 1234 needs to be output back to the user as 4321.

So I use the modular operation to get each digit:
1234 % 10 = 4
123 & 10 = 3
12 % 10 = 2
1 % 10 = 1

So I have the pattern 4,3,2,1. Without predefining a set number of digits for a user to enter, and without using loops, how would I display the number 4321 back to the user?

For example, I'd need to keep doing the modular of the function until the remainder is 0, but we aren't allowed to use loops. We also can't use any predefined java classes (like stringbuffer's .reverse()). Can anyone suggest some code?

Jimmy suggested using bit shifts but I can't understand it all that well and neither does he.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
How in fuck are you supposed to do it without a loop? >_>. I can't think of any way to do it without a loop.

Do you have to use the remainder to make it go backwards?
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Jesus.

Bitwise and Bit Shift Operators
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

Read. Understand. Use. >_>.
 
Jesus.



Read. Understand. Use. >_>.

I tried to understand. When I tried to use it it was bad.

I don't know how I'm supposed to do it without a loop...

and yeah the remainder is the number backwards.
 
Code:
    private static int reverse(int i)
    {
	int returnVal = 0;

	returnVal += ((i / 1) % 10) * 100000000;
	returnVal += ((i / 10) % 10) * 10000000;
	returnVal += ((i / 100) % 10) * 1000000;
	returnVal += ((i / 1000) % 10) * 100000;
	returnVal += ((i / 10000) % 10) * 10000;
	returnVal += ((i / 100000) % 10) * 1000;
	returnVal += ((i / 1000000) % 10) * 100;
	returnVal += ((i / 10000000) % 10) * 10;
	returnVal += ((i / 100000000) % 10) * 1;

	return returnVal;
    }
The above works, but looks like shit. The problem it has, though, is that it adds trailing zeros (123456 [AKA 000123456] becomes 654321000). It would be perfect if you made a method to remove the trailing zeros (normally I would, but I'm a bit strapped for time atm, will probably make that method when I get home from school).
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Wait. Dur. Ints have a max number they can be... Forgot about that. Good job Jimmmy.

But. You fail. Read your post. >_<.
 
duelarena.gg
Back
Top