Java Binary Encode toBin(byte n)

Here you can find the source of toBin(byte n)

Description

to Bin

License

Open Source License

Declaration

public static String toBin(byte n) 

Method Source Code

//package com.java2s;
/*//from   w ww .  j  a  va  2  s . c  o  m
 * SubChannel - A study on subliminal channels in DSA algorithm.
 * Copyright (C) 2016 Thomas Leplus
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String toBin(byte n) {
        String s = "";
        for (int i = 7; i >= 0; i--)
            s += (n >> i) & 0x01;
        return s;
    }

    public static String toBin(short n) {
        return toBin((byte) (n >>> 8)) + toBin((byte) n);
    }

    public static String toBin(int n) {
        return toBin((short) (n >>> 16)) + toBin((short) n);
    }

    public static String toBin(long n) {
        return toBin((int) (n >>> 32)) + toBin((int) n);
    }
}

Related

  1. toBin(int bin, int size)
  2. toBin(int size, String bitString)
  3. toBin(int x)
  4. toBin(long value, int width)