From 6d61ab37611fe39c8f753d32c14ee54605983541 Mon Sep 17 00:00:00 2001 From: Yadukrishnan Date: Thu, 25 Apr 2024 09:44:27 +0530 Subject: [PATCH] Added another approach for the calculation --- .../twoscomplement/TwosComplement.java | 36 +++++++++++++++++++ .../TwosComplementUnitTest.java | 15 ++++++++ 2 files changed, 51 insertions(+) diff --git a/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/twoscomplement/TwosComplement.java b/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/twoscomplement/TwosComplement.java index 0bff5abc11..094114422f 100644 --- a/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/twoscomplement/TwosComplement.java +++ b/core-java-modules/core-java-numbers-7/src/main/java/com/baeldung/twoscomplement/TwosComplement.java @@ -1,6 +1,7 @@ package com.baeldung.twoscomplement; import java.math.BigInteger; +import java.util.stream.Collectors; public class TwosComplement { @@ -74,6 +75,41 @@ public class TwosComplement { return number.compareTo(minValue) >= 0 && number.compareTo(maxValue) <= 0; } + public static String decimalToTwosComplementBinaryUsingShortCut(BigInteger num, int numBits) { + if (!canRepresentInNBits(num, numBits)) { + throw new IllegalArgumentException(numBits + " bits is not enough to represent the number " + num); + } + var isNegative = num.signum() == -1; + var absNum = num.abs(); + + // Convert the abs value of the number to its binary representation + String binary = absNum.toString(2); + + // Pad the binary representation with zeros to make it numBits long + while (binary.length() < numBits) { + binary = "0" + binary; + } + + // If the input number is negative, calculate two's complement + if (isNegative) { + binary = performTwosComplementUsingShortCut(binary); + } + + return formatInNibbles(binary); + } + + private static String performTwosComplementUsingShortCut(String binary) { + int firstOneIndexFromRight = binary.lastIndexOf('1'); + if (firstOneIndexFromRight == -1) { + return binary; + } + String rightPart = binary.substring(firstOneIndexFromRight); + String leftPart = binary.substring(0, firstOneIndexFromRight); + String leftWithOnes = leftPart.chars().mapToObj(c -> c == '0' ? '1' : '0') + .map(String::valueOf).collect(Collectors.joining("")); + return leftWithOnes + rightPart; + } + } diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/twoscomplement/TwosComplementUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/twoscomplement/TwosComplementUnitTest.java index 248727f2ab..d48a010ffe 100644 --- a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/twoscomplement/TwosComplementUnitTest.java +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/twoscomplement/TwosComplementUnitTest.java @@ -24,6 +24,21 @@ public class TwosComplementUnitTest { Assertions.assertEquals(expected, twosComplement); } + @ParameterizedTest(name = "Twos Complement of {0} with number of bits {1}") + @CsvSource({ + "0, 4, 0000", + "1, 4, 0001", + "-1, 4, 1111", + "7, 4, 0111", + "-7, 4, 1001", + "12345, 16, 0011 0000 0011 1001", + "-12345, 16, 1100 1111 1100 0111" + }) + public void givenNumberAndBits_getTwosComplementUsingShortcut(String number, int noOfBits, String expected) { + String twosComplement = TwosComplement.decimalToTwosComplementBinary(new BigInteger(number), noOfBits); + Assertions.assertEquals(expected, twosComplement); + } + @Test public void givenNumberOutsideBitsRange_throwException() { Exception ex = Assertions.assertThrows(IllegalArgumentException.class, () -> {