Java Number Divide divmod(byte[] number, int firstDigit, int base, int divisor)

Here you can find the source of divmod(byte[] number, int firstDigit, int base, int divisor)

Description

Divides a number, represented as an array of bytes each containing a single digit in the specified base, by the given divisor.

License

Apache License

Parameter

Parameter Description
number the number to divide
firstDigit the index within the array of the first non-zero digit (this is used for optimization by skipping the leading zeros)
base the base in which the number's digits are represented (up to 256)
divisor the number to divide by (up to 256)

Return

the remainder of the division operation

Declaration

private static byte divmod(byte[] number, int firstDigit, int base, int divisor) 

Method Source Code

//package com.java2s;
/*/*  w ww  . ja  v a  2 s.c om*/
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Divides a number, represented as an array of bytes each containing a single digit
     * in the specified base, by the given divisor. The given number is modified in-place
     * to contain the quotient, and the return value is the remainder.
     *
     * @param number the number to divide
     * @param firstDigit the index within the array of the first non-zero digit
     *        (this is used for optimization by skipping the leading zeros)
     * @param base the base in which the number's digits are represented (up to 256)
     * @param divisor the number to divide by (up to 256)
     * @return the remainder of the division operation
     */
    private static byte divmod(byte[] number, int firstDigit, int base, int divisor) {
        // this is just long division which accounts for the base of the input digits
        int remainder = 0;
        for (int i = firstDigit; i < number.length; i++) {
            int digit = (int) number[i] & 0xFF;
            int temp = remainder * base + digit;
            number[i] = (byte) (temp / divisor);
            remainder = temp % divisor;
        }
        return (byte) remainder;
    }
}

Related

  1. divideAndCeilWithBase(int number, int base)
  2. divideAndRoundDown(long number, long divisor)
  3. divideBy(Number number1, Number number2)
  4. divideByRoundDown(Number number1, Number number2)
  5. division(Number numerator, Number denominator)
  6. divmod256(byte[] number58, int startAt)
  7. divRate(double v1, double v2)
  8. isDivisible(Double a, int b, int decimal)