fast binary calculator
From: Andre Coelho
Date: Sat Jul 04 2020 - 20:40:19 EST
ey
Ive made a program, that uses bit flips to quickly represent binary numbers
for instance, if i use a 128bit number than the algorithm uses
128+128+128 (and not 128*128)
for each bit.
Basically, it assigns slots to bits, so for instance
pos 1ÂÂÂ pos 2 pos 3
0ÂÂÂ ÂÂÂ ÂÂÂÂ 0ÂÂÂÂÂÂ 0
1ÂÂÂ ÂÂÂ ÂÂÂ 1ÂÂÂÂÂ 1
so that reduces the time it takes to scan them. I also use bit flips to
change the bit on a particular slot.
i wrote both c and java files.
later
Here is the code in c
#include <stdio.h>
char flipBit(char c);
char flipBit(char c) {
ÂÂÂ if (c == '0')
ÂÂÂÂÂÂÂ return '1';
ÂÂÂ else
ÂÂÂÂÂÂÂ return '0';
}
int main() {
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ char binaryNumber[] = { '0','0','0','0',0 };
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ int slot1,slot2,slot3,slot4;
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ for (slot1=0 ; slot1 < 2 ; slot1++) {
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ printf("\n%s",binaryNumber);
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ for (slot2 =1 ; slot2 < 2 ; slot2++) {
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ binaryNumber[slot2] =
flipBit(binaryNumber[slot2]);
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ printf("\n%s",binaryNumber);
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ for (slot3=2 ; slot3 < 3 ; slot3++) {
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ binaryNumber[slot3] =
flipBit(binaryNumber[slot3]);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ printf("\n%s",binaryNumber);
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ for (slot4=3 ; slot4 < 4 ; slot4++) {
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ binaryNumber[slot4] =
flipBit(binaryNumber[slot4]);
printf("\n%s",binaryNumber);
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ }
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ }
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ }
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ binaryNumber[slot1] =
flipBit(binaryNumber[slot1]);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ printf("\n");
ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ ÂÂÂ }
}
--
Andrà Albergaria Coelho
andrealbergaria@xxxxxxxxx
#include <stdio.h>
char flipBit(char c);
char flipBit(char c) {
if (c == '0')
return '1';
else
return '0';
}
int main() {
char binaryNumber[] = { '0','0','0','0',0 };
int slot1,slot2,slot3,slot4;
for (slot1=0 ; slot1 < 2 ; slot1++) {
printf("\n%s",binaryNumber);
for (slot2 =1 ; slot2 < 2 ; slot2++) {
binaryNumber[slot2] = flipBit(binaryNumber[slot2]);
printf("\n%s",binaryNumber);
for (slot3=2 ; slot3 < 3 ; slot3++) {
binaryNumber[slot3] = flipBit(binaryNumber[slot3]);
printf("\n%s",binaryNumber);
for (slot4=3 ; slot4 < 4 ; slot4++) {
binaryNumber[slot4] = flipBit(binaryNumber[slot4]);
printf("\n%s",binaryNumber);
}
}
}
binaryNumber[slot1] = flipBit(binaryNumber[slot1]);
printf("\n");
}
}
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class binPath {
// ao inves de se usar o numero , usas a posicao dele na posicao do numero (na array)
// ao inves de se usar o sistema de numeros (cada posicao ocupa um elemento vezes a base)
public static void main(String[] args) {
byte[] plaintext = {0,0,0,1,1,1,0,1};
// prints bits and positions
/*byte[] b = {0,0,0,0,0,0,0,0};
byte[] b2 = {1,1,1,1,1,1,1,1};
for (int pos=0; pos < 8; pos++) {
System.out.println("position "+pos+" "+b[pos]);
System.out.println("position "+pos+" "+b2[pos]);
}
*/
byte[] t = { 0,0,0,0 };
for (int slot1 = 0 ; slot1 < 2 ; slot1++) {
System.out.println();
System.out.print(Arrays.toString(t));
for (int slot2 = 1; slot2 < 2 ; slot2++) {
t[slot2] = bitFlip(t[slot2]);
System.out.println();
System.out.print(Arrays.toString(t));
for (int slot3 = 2; slot3 < 3 ; slot3++) {
t[slot3] = bitFlip(t[slot3]);
System.out.println();
System.out.print(Arrays.toString(t));
for (int slot4=3; slot4 < 4 ; slot4++) {
t[slot4] = bitFlip(t[slot4]);
System.out.println();
System.out.print(Arrays.toString(t));
}
}
}
t[slot1] = bitFlip(t[slot1]);
}
}
// Flips bits
public static byte bitFlip(byte b) {
if (b == 0)
return 1;
else
return 0;
}
}