Skip to content

Instantly share code, notes, and snippets.

@Deviad
Last active January 30, 2019 00:43
Show Gist options
  • Save Deviad/7bf95f1b94221a3f22baee4a9fe3889a to your computer and use it in GitHub Desktop.
Save Deviad/7bf95f1b94221a3f22baee4a9fe3889a to your computer and use it in GitHub Desktop.
BitSum
package com.davide;
/*
# funzione sommabit: effettua somme di bit ricorsivamente. Vuole in input: il primo numero da sommare, il secondo numero da sommare, il riporto al passo precedente e la posizione a cui stiamo sommando
def sommabit(a, b, riportoprecedente, posizione):
if posizione == 0:
s = (not (a) and b) or (a and not (b))
r = a and b
return s, r, 0
elif posizione == 1:
s = ((not (a)) and (not (b)) and riportoprecedente) or ((not (a)) and (b) and (not (riportoprecedente))) or (a and (not (b))and (not (riportoprecedente))) or (a and b and riportoprecedente)
r = (b and riportoprecedente) or (a and riportoprecedente) or (a and b)
return s, r, riportoprecedente
else:
s, r, riportoprecedente = sommabit(a, b, riportoprecedente, (posizione - 1))
return s, r, riportoprecedente
*/
import java.util.BitSet;
public class BitCalculator {
public static BitCalculatorResult sum(final BitSet a, final BitSet b, BitSet carry, Integer pos) {
if(carry == null) {
carry = BitUtils.fromInteger(0);
}
if(pos == null) {
pos = 0;
}
BitSet first = BitUtils.fromInteger(0);
BitSet second = BitUtils.fromInteger(0);
BitSet aCopyOne = (BitSet)a.clone();
BitSet bCopyOne = (BitSet)b.clone();
BitSet aCopyTwo = (BitSet)a.clone();
BitSet bCopyTwo = (BitSet)b.clone();
BitSet aCopyThree = (BitSet)a.clone();
BitSet bCopyThree = (BitSet)b.clone();
if(pos == 0) {
bCopyOne.andNot(aCopyOne);
aCopyTwo.andNot(bCopyTwo);
bCopyOne.or(aCopyOne);
aCopyThree.and(bCopyThree);
first = bCopyOne;
second = aCopyThree;
} else if (pos == 1) {
}
return new BitCalculatorResult(first, second, carry, pos);
}
}
package com.davide;
import lombok.Data;
import java.util.BitSet;
@Data
public class BitCalculatorResult {
final BitSet first;
final BitSet second;
final BitSet carry;
final Integer pos;
}
package com.davide;
import java.util.BitSet;
class BitUtils {
public static BitSet fromString(final String s) {
return BitSet.valueOf(new long[] { Long.parseLong(s, 2) });
}
public static BitSet fromInteger(final Integer n) {
return BitSet.valueOf(new long[]{n});
}
public static String toString(BitSet bs) {
return Long.toString(bs.toLongArray()[0], 2);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.davide</groupId>
<artifactId>bitsum</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-log</artifactId>
<version>0.17.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment