Skip to content

Instantly share code, notes, and snippets.

@Marahin
Last active August 29, 2015 14:10
Show Gist options
  • Save Marahin/8399795d85f6a235a297 to your computer and use it in GitHub Desktop.
Save Marahin/8399795d85f6a235a297 to your computer and use it in GitHub Desktop.
U2 / 2
#include <cstdio>
#include <iostream>
#include <bitset>
using namespace std;
string simple_2(unsigned long long int liczba){
/* ta funkcja ucina niepotrzebne zera, tak, aby ciag byl ciagiem systemu dwojkowego (przyjmuje tylko liczby naturalne, w systemie 10-nym) */
string binary = bitset<8>(liczba).to_string(); /* konwertujemy liczbe na binarna (o wielkosci 16 bitow) i zamieniamy w string (bo normalne liczby nie moga zaczynac sie od "0" :)) */
string new_binary;
bool received = false; /* zmienna ktora definiuje, czy skonczylismy ucinac zera z poczatku */
for (unsigned int x = 0; x < binary.length(); x++){
if (binary[x] != '0' && !received ){
new_binary += binary[x];
received = true;
}
else if (received){
new_binary += binary[x];
}
}
return new_binary;
}
int main(int argc, char* argv[]){
unsigned long long int liczba = 0; /* liczba naturalna ktora poda nam uzytkownik */
cout << "Liczba calkowita (10): ";
cin >> liczba;
string binary = simple_2(liczba);
cout << liczba << " w (2): " << binary << endl;
return 0;
}
#include <cstdio>
#include <iostream>
#include <bitset>
using namespace std;
string simple_2(unsigned long long int liczba){
/* ta funkcja ucina niepotrzebne zera, tak, aby ciag byl ciagiem systemu dwojkowego (przyjmuje tylko liczby naturalne, w systemie 10-nym) */
string binary = bitset<8>(liczba).to_string(); /* konwertujemy liczbe na binarna (o wielkosci 16 bitow) i zamieniamy w string (bo normalne liczby nie moga zaczynac sie od "0" :)) */
string new_binary;
bool received = false; /* zmienna ktora definiuje, czy skonczylismy ucinac zera z poczatku */
for (unsigned int x = 0; x < binary.length(); x++){
if (binary[x] != '0' && !received ){
new_binary += binary[x];
received = true;
}
else if (received){
new_binary += binary[x];
}
}
return new_binary;
}
string increment(string binary){
/* ta funkcja zgodnie z algorytmem musi zwiekszyc modul z ciagu binarnego o jeden - wtedy uzyskamy ciag ktory jest rowny liczbie ujemnej w systemie dziesietnym */
/* przyjmuje tylko odwrocony ciag binarny z funkcji u2 */
bool next = true;
for (unsigned int x = binary.length() - 1; x > 0; x--){
if (binary[x] == '1' && next == true){
binary[x] = '0';
next = true;
}
else if (binary[x] == '0' && next == true){
binary[x] = '1';
next = false;
}
}
return binary;
}
string flip(string binary){
cout << "negujemy" << endl;
/* negujemy nasz ciag uzyskane z simple_2 - czyli odwracamy kazde zero na 1 i odwrotnie */
for (unsigned int x = 0; x < binary.length(); x++){
if (binary[x] == '0'){
binary[x] = '1';
}
else if (binary[x] == '1'){
binary[x] = '0';
}
cout << binary << endl;
}
return binary;
}
string u2(long long int liczba){
string binary = simple_2(-(liczba));
cout << "Liczba |" << liczba << "| w systemie (2): " << binary << endl;
binary = flip(binary); /* patrz opis funkcji flip */
cout << "Zanegowany ciag: " << binary << endl;
binary = increment(binary);
cout << "Ciag powiekszony o 1: " << binary << endl;
return binary;
}
int main(int argc, char* argv[]){
long long int liczba = 0; /* liczba ktora poda nam uzytkownik */
cout << "Liczba calkowita (10): ";
cin >> liczba;
string binary; /* nasz ciag liczb w u2 / 2 */
if (liczba >= 0){ binary = simple_2(liczba); }
else{ binary = u2(liczba); }
cout << liczba << " w systemie ";
if (liczba >= 0) cout << "2: " << binary << endl;
else cout << "U2: " << binary << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment