MilaDimitrovaa

Exercise

Nov 10th, 2021
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static int fromBintoDec(char[] bin) {
  7.         int number = 0;
  8.  
  9.         for (int i = 0; i < bin.length; i++) {
  10.             if(bin[i] == '1'){
  11.                 number += Math.pow(2, bin.length - 1 - i);
  12.             }
  13.         }
  14.  
  15.         return number;
  16.     }
  17.  
  18.     public static String fromCharToString(char[] arr){
  19.         String output = "";
  20.  
  21.         for (char ch : arr) {
  22.             output += ch;
  23.         }
  24.  
  25.         return output;
  26.     }
  27.  
  28.  
  29.     public static int howManyZerosInBinary(char[] bin){
  30.         int counter = 0;
  31.  
  32.         for (int i = 0; i < bin.length; i++) {
  33.             if(bin[i] == '0'){
  34.                 counter++;
  35.             }
  36.         }
  37.  
  38.         return counter;
  39.     }
  40.  
  41.  
  42.     public static int sumOfOnesInBinary(char[] bin){
  43.         int counter = 0;
  44.  
  45.         for (int i = 0; i < bin.length; i++) {
  46.             if(bin[i] == '1'){
  47.                 counter++;
  48.             }
  49.         }
  50.  
  51.         return counter;
  52.     }
  53.  
  54.  
  55.     public static char[] fromDecToBinary(int number) {
  56.         int elementsInBinary = 0;
  57.  
  58.         if(number < 0){
  59.             number *= -1;
  60.         }
  61.  
  62.         char[] binary;
  63.  
  64.         while(number > Math.pow(2, elementsInBinary)){
  65.             elementsInBinary++;
  66.         }
  67.         binary = new char[elementsInBinary];
  68.  
  69.  
  70.         for (int i = 0; i < binary.length; i++) {
  71.             int powerOfTwo = (int)Math.pow(2, binary.length - 1 - i);
  72.  
  73.             if(number - powerOfTwo >= 0){
  74.                 binary[i] = '1';
  75.                 number -= powerOfTwo;
  76.             }
  77.             else{
  78.                 binary[i] = '0';
  79.             }
  80.         }
  81.  
  82.         return binary;
  83.     }
  84.  
  85.  
  86.  
  87.     public static char[] straightCodeOfDecimalNumber(int number){
  88.         boolean numberIsNegative = number < 0;
  89.         char[] numberAsBinary = fromDecToBinary(number);
  90.         char[] straightCode = new char[numberAsBinary.length + 1];
  91.  
  92.         if(numberIsNegative)
  93.         {
  94.             straightCode[0] = '1';
  95.         }else{
  96.             straightCode[0] = '0';
  97.         }
  98.  
  99.         for (int i = 1; i < straightCode.length; i++) {
  100.             straightCode[i] = numberAsBinary[i - 1];
  101.         }
  102.  
  103.         return straightCode;
  104.     }
  105.  
  106.  
  107.     public static char[] reversedCodeFromDecimalNumber(int number){
  108.         boolean numberIsNegative = number < 0;
  109.         char[] numberAsBinary = fromDecToBinary(number);
  110.         char[] reversedCode = new char[numberAsBinary.length + 1];
  111.  
  112.         if(numberIsNegative)
  113.         {
  114.             reversedCode[0] = '1';
  115.  
  116.             for (int i = 1; i < reversedCode.length; i++) {
  117.                 if(numberAsBinary[i - 1] == '1') {
  118.                     reversedCode[i] = '0';
  119.                 }else{
  120.                     reversedCode[i] = '1';
  121.                 }
  122.             }
  123.  
  124.         }else{
  125.             reversedCode[0] = '0';
  126.  
  127.             for (int i = 1; i < reversedCode.length; i++) {
  128.                 reversedCode[i] = numberAsBinary[i - 1];
  129.             }
  130.         }
  131.  
  132.  
  133.  
  134.         return reversedCode;
  135.     }
  136.  
  137.  
  138.     public static String sumOfBinaryNumber(String n1, String n2) {
  139.         int length = Math.max(n1.length(), n2.length());
  140.         String sum;
  141.  
  142.         int temp = Integer.parseInt(n1, 2) + Integer.parseInt(n2, 2);
  143.  
  144.         sum = Integer.toBinaryString(temp);
  145.  
  146.         System.out.println(sum);
  147.  
  148.         return sum;
  149.     }
  150.  
  151.  
  152.     public static char[] additionalCodeFromDecimalNumber(int number){
  153.         boolean numberIsNegative = number < 0;
  154.         char[] numberAsBinary = fromDecToBinary(number);
  155.         ArrayList<Character> additionalCode = new ArrayList<>();
  156.  
  157.         if(numberIsNegative) {
  158.             for (int i = 0; i < numberAsBinary.length; i++) {
  159.                 if(numberAsBinary[i] == '1') {
  160.                     numberAsBinary[i] = '0';
  161.                 }else{
  162.                     numberAsBinary[i] = '1';
  163.                 }
  164.             }
  165.  
  166.             char[] temp = sumOfBinaryNumber(fromCharToString(numberAsBinary), "1").toCharArray();
  167.  
  168.             additionalCode.add('1');
  169.             for (char ch : temp) {
  170.                 additionalCode.add(ch);
  171.             }
  172.  
  173.         }else{
  174.             additionalCode.add('0');
  175.  
  176.             for (char ch : numberAsBinary) {
  177.                 additionalCode.add(ch);
  178.             }
  179.         }
  180.  
  181.         char[] output = new char[additionalCode.size()];
  182.  
  183.         for (int i = 0; i < output.length; i++) {
  184.             output[i] = additionalCode.get(i);
  185.         }
  186.  
  187.         return output;
  188.     }
  189.  
  190.  
  191.     public static int subtractWithoutOperator(int a, int b){
  192.         int c = 0;
  193.         int result = 0;
  194.         boolean swap = false;
  195.  
  196.         if(a < b){
  197.             int temp = a;
  198.             a = b;
  199.             b = temp;
  200.             swap = true;
  201.         }
  202.  
  203.         c = a ^ b;
  204.         c = c & b;
  205.         c = c << 1;
  206.         a = a ^ b;
  207.         result = a ^ c;
  208.  
  209.         if(swap){
  210.             result *= -1;
  211.         }
  212.  
  213.  
  214.         return result;
  215.     }
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment