Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class FunctionsBIN {
- public static int fromBinToDec(char[] bin){
- int number = 0;
- for (int i = 0; i < bin.length; i++) {
- if(bin[i] == '1'){
- number += Math.pow(2, bin.length - 1 - i);
- }
- }
- return number;
- }
- public static String fromCharToString(char[] arr){
- String output = "";
- for (char ch : arr) {
- output += ch;
- }
- return output;
- }
- public static int howManyZerosInBinary(char[] bin){
- int counter = 0;
- for (int i = 0; i < bin.length; i++) {
- if(bin[i] == '0'){
- counter++;
- }
- }
- return counter;
- }
- public static int sumOfOnesInBinary(char[] bin){
- int counter = 0;
- for (int i = 0; i < bin.length; i++) {
- if(bin[i] == '1'){
- counter++;
- }
- }
- return counter;
- }
- public static char[] fromDecToBinary(int number) {
- int elementsInBinary = 0;
- if(number < 0){
- number *= -1;
- }
- char[] binary;
- while(number > Math.pow(2, elementsInBinary)){
- elementsInBinary++;
- }
- binary = new char[elementsInBinary];
- for (int i = 0; i < binary.length; i++) {
- int powerOfTwo = (int)Math.pow(2, binary.length - 1 - i);
- if(number - powerOfTwo >= 0){
- binary[i] = '1';
- number -= powerOfTwo;
- }
- else{
- binary[i] = '0';
- }
- }
- return binary;
- }
- public static char[] straightCodeOfDecimalNumber(int number){
- boolean numberIsNegative = number < 0;
- char[] numberAsBinary = fromDecToBinary(number);
- char[] straightCode = new char[numberAsBinary.length + 1];
- if(numberIsNegative)
- {
- straightCode[0] = '1';
- }else{
- straightCode[0] = '0';
- }
- for (int i = 1; i < straightCode.length; i++) {
- straightCode[i] = numberAsBinary[i - 1];
- }
- return straightCode;
- }
- public static char[] reversedCodeFromDecimalNumber(int number){
- boolean numberIsNegative = number < 0;
- char[] numberAsBinary = fromDecToBinary(number);
- char[] reversedCode = new char[numberAsBinary.length + 1];
- if(numberIsNegative)
- {
- reversedCode[0] = '1';
- for (int i = 1; i < reversedCode.length; i++) {
- if(numberAsBinary[i - 1] == '1') {
- reversedCode[i] = '0';
- }else{
- reversedCode[i] = '1';
- }
- }
- }else{
- reversedCode[0] = '0';
- for (int i = 1; i < reversedCode.length; i++) {
- reversedCode[i] = numberAsBinary[i - 1];
- }
- }
- return reversedCode;
- }
- public static String sumOfBinaryNumber(String n1, String n2) {
- int length = Math.max(n1.length(), n2.length());
- String sum;
- int temp = Integer.parseInt(n1, 2) + Integer.parseInt(n2, 2);
- sum = Integer.toBinaryString(temp);
- System.out.println(sum);
- return sum;
- }
- public static char[] additionalCodeFromDecimalNumber(int number){
- boolean numberIsNegative = number < 0;
- char[] numberAsBinary = fromDecToBinary(number);
- ArrayList<Character> additionalCode = new ArrayList<>();
- if(numberIsNegative) {
- for (int i = 0; i < numberAsBinary.length; i++) {
- if(numberAsBinary[i] == '1') {
- numberAsBinary[i] = '0';
- }else{
- numberAsBinary[i] = '1';
- }
- }
- char[] temp = sumOfBinaryNumber(fromCharToString(numberAsBinary), "1").toCharArray();
- additionalCode.add('1');
- for (char ch : temp) {
- additionalCode.add(ch);
- }
- }else{
- additionalCode.add('0');
- for (char ch : numberAsBinary) {
- additionalCode.add(ch);
- }
- }
- char[] output = new char[additionalCode.size()];
- for (int i = 0; i < output.length; i++) {
- output[i] = additionalCode.get(i);
- }
- return output;
- }
- public static int subtractWithoutOperator(int a, int b){
- int c = 0;
- int result = 0;
- boolean swap = false;
- if(a < b){
- int temp = a;
- a = b;
- b = temp;
- swap = true;
- }
- c = a ^ b;
- c = c & b;
- c = c << 1;
- a = a ^ b;
- result = a ^ c;
- if(swap){
- result *= -1;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment