Come stampare (usando Cout) un numero in forma binaria?
-
27-10-2019 - |
Domanda
Sto seguendo un corso universitario sui sistemi operativi e stiamo imparando a convertire da binario a esadecimale, decimale a esadecimale, ecc. E oggi abbiamo appena imparato come i numeri firmati/non firmati vengono archiviati in memoria usando il complemento dei due (~ numero + 1).
Abbiamo un paio di esercizi da fare sulla carta e vorrei poter verificare le mie risposte prima di inviare il mio lavoro all'insegnante. Ho scritto un programma C ++ per i primi esercizi, ma ora sono bloccato su come potrei verificare la mia risposta con il seguente problema:
char a, b;
short c;
a = -58;
c = -315;
b = a >> 3;
E dobbiamo mostrare la rappresentazione binaria in memoria di a
, b
e c
.
L'ho fatto su carta e mi dà i seguenti risultati (tutte le rappresentazioni binarie in memoria dei numeri dopo il complemento dei due):
A = 00111010 (è un carattere, quindi 1 byte)
B = 00001000 (è un carattere, quindi 1 byte)
c = 11111110 11000101 (è un breve, quindi 2 byte)
C'è un modo per verificare la mia risposta? Esiste un modo standard in C ++ per mostrare la rappresentazione binaria in memoria di un numero, o devo codificare ogni passo da solo (calcolare il complemento dei due e quindi convertire in binario)? So che quest'ultimo non richiederebbe così tanto tempo, ma sono curioso di sapere se esiste un modo standard per farlo.
Soluzione
The easiest way is probably to create an std::bitset
representing the value, then stream that to cout
.
#include <bitset>
...
char a = -58;
std::bitset<8> x(a);
std::cout << x << '\n';
short c = -315;
std::bitset<16> y(c);
std::cout << y << '\n';
Altri suggerimenti
Use on-the-fly conversion to std::bitset
. No temporary variables, no loops, no functions, no macros.
#include <iostream>
#include <bitset>
int main() {
int a = -58, b = a>>3, c = -315;
std::cout << "a = " << std::bitset<8>(a) << std::endl;
std::cout << "b = " << std::bitset<8>(b) << std::endl;
std::cout << "c = " << std::bitset<16>(c) << std::endl;
}
Prints:
a = 11000110
b = 11111000
c = 1111111011000101
If you want to display the bit representation of any object, not just an integer, remember to reinterpret as a char array first, then you can print the contents of that array, as hex, or even as binary (via bitset):
#include <iostream>
#include <bitset>
#include <climits>
template<typename T>
void show_binrep(const T& a)
{
const char* beg = reinterpret_cast<const char*>(&a);
const char* end = beg + sizeof(a);
while(beg != end)
std::cout << std::bitset<CHAR_BIT>(*beg++) << ' ';
std::cout << '\n';
}
int main()
{
char a, b;
short c;
a = -58;
c = -315;
b = a >> 3;
show_binrep(a);
show_binrep(b);
show_binrep(c);
float f = 3.14;
show_binrep(f);
}
Note that most common systems are little-endian, so the output of show_binrep(c)
is not the 1111111 011000101 you expect, because that's not how it's stored in memory. If you're looking for value representation in binary, then a simple cout << bitset<16>(c)
works.
Is there a standard way in C++ to show the binary representation in memory of a number [...]?
No. There's no std::bin
, like std::hex
or std::dec
, but it's not hard to output a number binary yourself:
You output the left-most bit by masking all the others, left-shift, and repeat that for all the bits you have.
(The number of bits in a type is sizeof(T) * CHAR_BIT
.)
Similar to what is already posted, just using bit-shift and mask to get the bit; usable for any type, being a template (only not sure if there is a standard way to get number of bits in 1 byte, I used 8 here).
#include<iostream>
#include <climits>
template<typename T>
void printBin(const T& t){
size_t nBytes=sizeof(T);
char* rawPtr((char*)(&t));
for(size_t byte=0; byte<nBytes; byte++){
for(size_t bit=0; bit<CHAR_BIT; bit++){
std::cout<<(((rawPtr[byte])>>bit)&1);
}
}
std::cout<<std::endl;
};
int main(void){
for(int i=0; i<50; i++){
std::cout<<i<<": ";
printBin(i);
}
}
Reusable function:
template<typename T>
static std::string toBinaryString(const T& x)
{
std::stringstream ss;
ss << std::bitset<sizeof(T) * 8>(x);
return ss.str();
}
Usage:
int main(){
uint16_t x=8;
std::cout << toBinaryString(x);
}
This works with all kind of integers.
#include <iostream>
#include <cmath> // in order to use pow() function
using namespace std;
string show_binary(unsigned int u, int num_of_bits);
int main()
{
cout << show_binary(128, 8) << endl; // should print 10000000
cout << show_binary(128, 5) << endl; // should print 00000
cout << show_binary(128, 10) << endl; // should print 0010000000
return 0;
}
string show_binary(unsigned int u, int num_of_bits)
{
string a = "";
int t = pow(2, num_of_bits); // t is the max number that can be represented
for(t; t>0; t = t/2) // t iterates through powers of 2
if(u >= t){ // check if u can be represented by current value of t
u -= t;
a += "1"; // if so, add a 1
}
else {
a += "0"; // if not, add a 0
}
return a ; // returns string
}
Using old C++ version, you can use this snippet :
template<typename T>
string toBinary(const T& t)
{
string s = "";
int n = sizeof(T)*8;
for(int i=n; i>=0; i--)
{
s += (t & (1 << i))?"1":"0";
}
return s;
}
int main()
{
char a, b;
short c;
a = -58;
c = -315;
b = a >> 3;
cout << "a = " << a << " => " << toBinary(a) << endl;
cout << "b = " << b << " => " << toBinary(b) << endl;
cout << "c = " << c << " => " << toBinary(c) << endl;
}
a = ã => 111000110
b = ° => 111111000
c = -315 => 11111111011000101
Here is the true way to get binary representation of a number:
unsigned int i = *(unsigned int*) &x;
Is this what you're looking for?
std::cout << std::hex << val << std::endl;