MilaDimitrovaa

Bin - functions

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