-4

so I have a problem, I can not figure it out and i will be glad if somebody can give me a hand with my code. So I am converting a Byte into Bit using INPUT and OUTPUT function and my problem is that i can not find a way to automatically use the result (the output) to calculate it in binary also. So if I want to convert 50 B to bits I will get 400 as a result and these 400 bits (numbit) i want them to show up like binary also. This is my code:

#include<iostream>
#include<conio.h>
using namespace std;

main()
{

int numB, numbit;   
int arr [100], i = 0,j;


//INPUT
cout<<"Please Enter the number of Bytes:";
cin>>numB;
cout<<"\n";

//Formula bytes into bits
numbit = numB * 8;

//OUTPUT
cout<<"Is equal to the number of bits:"<<numbit;
cout<<"\n";


    while (numbit>0) 
    {
        arr [i] = numbit%2;
        i++;
        numbit=numbit/2;
    }
    cout<<"Binary number is: ";
    for (j= i-1; j>=0; j--)
    {
        cout<< arr[j];

return 0;

system("Pause");
}
}
Bear-K
  • 41
  • 8
  • Please don't use tags for programming languages that have nothing to do with your question. – Ben Mar 18 '19 at 22:30
  • Not related, but before assuming that a byte is 8 bits, you should read this: https://stackoverflow.com/q/11600378/3723423 – Christophe Mar 18 '19 at 22:34
  • @Christophe thanx for the hint. – Bear-K Mar 18 '19 at 22:42
  • It is not fully clear if numBit is a size (e.g. number of bytes converted into number of bits) or if you just want to convert this number into a printable sequence of bits. – Christophe Mar 18 '19 at 22:45
  • @Ben Oh, what is the tag that is not related to my question exactly in your opinion? – Bear-K Mar 18 '19 at 22:47
  • @Christophe maybe I am not very clear in my question, sorry for that. All I want is to convert the result from numB * 8 (assuming 1 B = 8 bits) into number (which i did) and that number to convert automatically into binary code. – Bear-K Mar 18 '19 at 22:53
  • @Bear-K yes, but still... numBits is already stored in binary format in the memory. When you say "convert into binary code", do you mean printing the individual bits of the nulber ? – Christophe Mar 18 '19 at 22:55
  • @Christophe exactly, yes. – Bear-K Mar 18 '19 at 22:57
  • @Bear-K your code is C++ only. C# and C are different languages. If you tag them, the result is that people who are looking for C or C# questions will see a C++ question instead, and will probably downvote you or simply remove the tags themselves to reduce noise – alter_igel Mar 18 '19 at 22:58
  • @alterigel my mistake, haven't slept for a while and even forgot i putted C# and C. And by not seeing different language in the tags than C++ I asked the question what is tag not related to my question... somebody already removed them. xD I am planning on being more active here and will get used to the habitat and the rules quickly :) – Bear-K Mar 18 '19 at 23:04
  • Not an opinion, as pointed out you had C# and C tags on your post, so I (and presumably 3 other people) downvoted it. I just commented to let you know why it was downvoted. If you stick to using only relevant tags, then a post like this that asks a precise question and includes code showing an attempt to solve the problem would usually be up-voted, and upvoted questions usually get more attention. Just advice. – Ben Mar 19 '19 at 02:47

1 Answers1

1

Update: BONUS-Round now both with signed and unsigned numbers (be ware the signed representation depends on the implementation i.e. compiler, also the endianess, operating system, same for the length of unsigned)

#include <iostream>
#include <bitset>
#include <cstdint>

int main()
{
    {
        unsigned unsigned_number{};
        std::cout << "Please Enter an unsigned:";

        std::cin >> unsigned_number;
        std::bitset<CHAR_BIT * sizeof(unsigned)> bits{unsigned_number};

        std::cout << "\nIs equal to the number of bits:" << bits << "\n";
    }
    {
        int32_t signed_number{};
        std::cout << "Please enter a signed:";

        std::cin >> signed_number;

        std::cout << "\nIs equal to the number of bits:";
        for (int i = 0; i < 4; ++i) {
            auto byte = reinterpret_cast<uint8_t*> (&signed_number) + i;
            std::bitset<8> bits{*byte};
            std::cout << bits;
        }
        std::cout << "\n";
    }
}
Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • Bless you, it converts numbers into binary but I found a way to make it work for me. Thanx once again for your time, bro... – Bear-K Mar 18 '19 at 23:41