I want to parse a timestamp, a Long number like 14655**56648041**. I want to create from this Long number two other Long numbers, One with the last four numbers 8041 and the second with 5664. I can convert the number to String, use substring and then convert it again to Long, but I want to know if there is a cleaner way? I tried using % and / but didn't succeed.
The last 4 digits are num % 1E4 and the four previous ones are (num % 1E8 - (num % 1E4)) / 1E4
There is. You can obtain the last four numbers by performing a modulo 10000 on the number:
long last4 = all%10000;
You can furthermore obtain the middle four digits by first dividing by 10000 and the again perform modulo:
long midl4 = (all/10000)%10000;
Or putting it all together:
long all = 1465556648041L;
long last4 = all%10000;
long midl4 = (all/10000)%10000;
System.out.println("all="+all);
System.out.println("last4="+last4);
System.out.println("midl4="+midl4);
jDoodle demo
Related
I am new to Java and programming all together.. I am trying to make a program that ciphers a number for the user. The user inputs 5 digits separately so I add them together to get a total. I need to pull the first digit and second digit of the total and enter it into (firstDigit+key)%10 and (secondDigit+key)%10. Then need to combine the answer to each equation together.
My total needs to be two digits, so even if the user enters all 1's which would total to be 5, I need it to be displayed and seen as 05 so that both equations have a digit to use. It needs to be two digits. I cant seem to figure how to enter a place holder. I was thinking about trying to use:
if (total < 10)
but then what?
Secondly, the method I used below seems like a terrible way to pull a single digit from a number. I think I changed the int total into a string so I can use .substring to pull the digits, then converted back to an int. Surprisingly it works. Is there a better way to do this knowing that the number is random?
String totalString = Integer.toString(total);
String stringDigit1 = totalString.substring(0,1);
String stringDigit2 = totalString.substring(1,2);
int firstDigitInt1 = Integer.parseInt(stringDigit1);
int firstDigitInt2 = Integer.parseInt(stringDigit2);
int encodedDigit1 = (firstDigitInt1+key)%10;
int encodedDigit2 = (firstDigitInt2+key)%10;
System.out.println("Your encoded Number is: " + encodedDigit1 + encodedDigit2);
Your method for obtaining the individual digits is good, and if you want to maintain it I believe your intuition is correct, it should suffice to say:
if (total < 10) {
firstDigitInt1 = 0
}
This will work out with your following math.
Your method with substrings is far from terrible, but in case you wanted something more sleek you can instead say:
int Digit1 = total / 10;
int Digit2 = total % 10;
Here, you can take advantage of integer truncation (where an integer won't remember decimal places) to get the first digit, which also solves the first problem: 5 / 10 = 0 (in terms of ints). For the second digit, it suffices to say modulo 10, as it is the remainder once the total is divided by 10.
I need an explanation for this tiny java code.
decimalNumber is a variable for user input and M for roman numeral 1000
This is a part of a roman numeral converter:
m = decimalNumber / 1000;
decimalNumber = decimalNumber % 1000;
The / operator between two ints performs an integer division - i.e., it returns only the whole part of the division. The % operator is the modulo operator - it returns the remainder of the division.
So here, you use / to get the thousands part of the number, and then assign the remainder to it, presumably to continue the conversion of hundreds, tens, etc.
The first line m = decimalNumber / 1000; sets m equal to the number of thousands in decimalNumber.
Then, the second line, sets decimalNumber equal to decimalNumber mod 1000. (If you're unfamiliar with modulo, it's the remainder after division.)
So, for example, let's take the number 2453 to start with in decimalNumber. First, we set m equal to 2453 / 1000 which, in integer division, is 2. (Remember, the remainder is lost when dividing integers, but NOT when dividing floats.)
Then, decimalNumber is set equal to 2453 % 1000, which is 453.
By doing this, you're basically counting how many 'M's you need in your final number, then reducing decimalNumber by that many thousands.
I have a long that corresponds to an assembly language instruction.
Here's the problem; the first field is Opcode. It can be either 1 or 2 digits. So for example in 120602, 12 is the opcode. In 10602, 1 is the opcode.
I want to extract each individual field; where opcode is the first 1-2 numbers on the left, 1 to the right of that is op1mode, 1 to the right of that is op1gpr, 1 to the right of that is op2mode, and finally, the last part is op2gpr.
Ideally, I want to assign each to its own variable for later use, or separate them in an array.
I was thinking that this can be achievable using bitwise operations; namely masks and shifts.
How would one split the number with just bitwise operations?
Bitwise/bitshifts won't do you any good unless the fields are combined in a base 2 representation. As shown, your digits are base 10. On the other hand, you can use integer division and modulo for the numbers you've shown.
120602 / 10000 = 12
120602 % 10000 = 602
These basically correspond to the following types of operations for binary digits:
0x1D71A >>> 12 = 0x1D
0x1D71A & 0xFFF = 0x71A
An easy way to do that (but without bit manipulation), is to define an array containing five integers, and then to fill it which the digits of your number. You will have to begin from the end of the number, it is easier.
An example in Java:
long number = 120602;
int[] op = new int[5];
for(int i = 0; i < 4; ++i) {
op[i] = (int) (number % 10);
number /= 10;
}
op[4] = (int) number;
And here:
op[0] is op2gpr
op[1] is op2mode
op[2] is op1gpr
op[3] is op1mode
op[4] is opcode
I am making a program and i need a way to make variables go over 10 billion and int only stores up to 999 million for me so i decided to use a long instead of a int and it turn out it only stores up to 999 million as well.
int TotalWorldPop = 7200000000;
gives me the "literal is out of range" error
long TotalWorldPop = 7200000000;
gives me the "literal is out of range" error as well
but
int TotalWorldPop = 999999999
is ok for me
A long can accommodate numbers as large as 263-1. But there's a trick to putting them into the primitive field.
If you're entering the primitive literal, then you have to add an L at the end, as all numeric literals are treated as int (and it can only go up to ~2.1 billion).
If you need numbers larger than that, use BigInteger.
You could use a BigInteger to store very large numbers.
Example:
Biginteger bigInt1 = new Biginteger("91826581752671985235272769716");
Biginteger bigInt2 = new Biginteger("-1796357891266373473772242");
Biginteger bigint3 = bigInt1.divide(bigInt2);
Biginteger bigint4 = bigInt1.add(bigInt2);
Right, sorry I can't think of the correct words to google this. So I'll have to ask.
I've got a long (System.currentTimeMillis())
Lets say
3453646345345345
I want to remove the last six (or other number of) digits and I think I can do this doing some kind of bit shift?
so I would end up with
3453646345
EDIT
I wanted to get the System.currentTimeMillis() within a time box, so if I ask for the time then ask again 29 seconds later it will return the same number but if I ask 31 seconds later it will return a different number. With the 30 second timebox being configurable.
You have to simply divide it by 1M long shorter = System.currentTimeMillis() / 1000000L;
To build on #Yob's answer, you can make the number of digits to remove configurable by creating a method like this:
public long removeDigits(long number, int digitsToRemove) {
return number / (long)Math.pow(10, digitsToRemove);
}
Depending on what you want to do (in base 10, I assume), you can do this:
int64_t radix = 1000000; // or some other power of 10
x -= x%radix; // last 6 decimal digits are now 0
// e.g: from 3453646345345345 to 3453646345000000
Or this (as in the previous answer):
x /= radix; // last 6 decimal digits are gone, the result rounded down
// e.g: from 3453646345345345 to 3453646345
Response to Edit
For your purposes, you could change radix in the modulus example to 30000:
int64_t timeInterval = 30000;
displayTime = actualTime - (actualTime % timeInterval);
Where displayTime and actualTime are in milliseonds. displayTime will, in this case, have a (rounded-down) granularity of 30 seconds while remaining a unit of milliseconds.
To have a rounded up granularity, you can do the following:
int64_t timeInterval = 30000;
int64_t modulus = actualTime % timeInterval;
displayTime = actualTime - modulus + (modulus?timeInterval:0);
Though, based on what you are asking, it seems you just want to update a display value only every few ticks. The following will work as well:
if((actualTime - displayTime) >= timeInterval){
displayTime = actualTime - (actualTime % timeInterval);
}
Pardon the C integer types, I just prefer to be unambiguous about the width of integer I'm using :P.