Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Demo4 {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.println("Въведи число: ");
  8.         int num = sc.nextInt();
  9.         System.out.println(Integer.toBinaryString(num));
  10.         int numOfBit = 0;
  11.         int bitCount = num;
  12.         byte count = 0;
  13.         while (bitCount > 0) {
  14.             bitCount /= 2;
  15.             count++;
  16.         }
  17.         int cons = numOfBit + count;
  18.         while (count > numOfBit) {
  19.             boolean one = (num & (1 << (cons-1))) != 0;
  20.             numOfBit++;
  21.             cons--;
  22.             if (one == true) {
  23.                 System.out.print("1");
  24.             }
  25.             if (one == false) {
  26.                 System.out.print("0");
  27.             }
  28.         }
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement