Java Binary Encode toBinaryString(int number)

Here you can find the source of toBinaryString(int number)

Description

Returns a string representation of the integer argument as an unsigned integer in base 2.

License

Open Source License

Parameter

Parameter Description
number an integer to be converted to a string

Return

the string representation of the unsigned integer value represented in binary (base 2)

Declaration

public static String toBinaryString(int number) 

Method Source Code

//package com.java2s;
/**/*from ww  w  .j  a v a 2 s  .com*/
 * The MIT License (MIT)
 *
 * Copyright (C) 2015 Luka Obradovic.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    private static final int FIRST_BIT_MASK = 1;

    /**
     * Returns a string representation of the integer argument as an unsigned
     * integer in base 2. Returned string has all 32 digits including leading
     * zeros.
     *
     * @param number an integer to be converted to a string
     * @return the string representation of the unsigned integer value
     *         represented in binary (base 2)
     */
    public static String toBinaryString(int number) {
        String bin = "";

        for (int i = 1; i < Integer.SIZE; ++i) {
            if (i % 8 == 0) {
                bin = " " + (number & FIRST_BIT_MASK) + bin;
            } else {
                bin = (number & FIRST_BIT_MASK) + bin;
            }

            number = number >> 1;
        }

        bin = (number & FIRST_BIT_MASK) + bin;
        return bin;
    }

    /**
     * Returns a string representation of the long argument as an unsigned long
     * in base 2. Returned string has all 64 digits including leading zeros.
     *
     * @param number an long to be converted to a string
     * @return the string representation of the unsigned long value
     *         represented in binary (base 2)
     */
    public static String toBinaryString(long number) {
        String bin = "";

        for (int i = 1; i < Long.SIZE; ++i) {
            if (i % 8 == 0) {
                bin = " " + (number & FIRST_BIT_MASK) + bin;
            } else {
                bin = (number & FIRST_BIT_MASK) + bin;
            }

            number = number >> 1;
        }

        bin = (number & FIRST_BIT_MASK) + bin;
        return bin;
    }
}

Related

  1. toBinaryString(final byte number)
  2. toBinaryString(final char[] arr)
  3. toBinaryString(int a, int bits)
  4. toBinaryString(int b, int bits)
  5. toBinaryString(int l, int L)
  6. toBinaryString(int number, int length)
  7. toBinaryString(int value)
  8. toBinaryString(int[] input)
  9. toBinaryString(long val)