0
#include <iostream> using namespace std; int main() { long int b2 = 0; long int i = 1; int x; cout << "Enter a number:" ; cin >> x ; int y = x; while (x!=0) { if ( x%2 ) { b2 = b2 + i; } i = i*10; x=x/2; } cout << "Number " << y << " in base(2) is: " << b2; } 

This code transforms any number from base 10 to binary. There is only one problem, if I change any of the variable b2 or i to int instead of long int and I insert a number great than 1000 I get some weird results, sometimes negatives. I'm using Ubuntu 18.04 and code::blocks to compile. I researched but couldn't really find an answer. The int has 4 bytes which means 2^32 possibilities. It should work...

7
  • As explained here standard defines int to be at least 16-bit long, while it defines long int to be at least 32-bits long. Commented Sep 24, 2018 at 17:06
  • 4
    Understand that when you "encode" the binary representation of a number in an int, you are actually writing a base 10 number using only 1 and 0. You are wasting the vast majority of your 2^32 possibilities. Commented Sep 24, 2018 at 17:06
  • 3
    I recommend you store the binary "number" as a string instead. Commented Sep 24, 2018 at 17:06
  • 4
    i = i*10; That can get really large really fast. Commented Sep 24, 2018 at 17:08
  • 2
    INT_MAX = 2,147,483,647 so after 9 bits you'll overflow since 10^10 = 10,000,000,000. Commented Sep 24, 2018 at 17:12

2 Answers 2

4

You're confusing numbers with numerals.

A (32-bit) int (by far the most common these days) can hold 2^32 (about 4 billion) possible values. But each of those values is a number. 0b10000, 0x10, 020, and 16 are different numerals, but as an int, they're all the same number. There's no conversion to be done at that level.

What you're doing is trying to use numbers as numerals. And that is going to cause you headaches. By multiplying by 10 to shift, you effectively use decimal digits as bits. A decimal digit is worth a little over 3 bits (log210 ~= 3.322), meaning your 32-bit number can only represent 10 bits without problems. Any value over 1023 will end up becoming an 11-digit number and overflowing your int.

Using long int instead can get you another 32 bits, if your environment has 64-bit longs. That'll get your limit up to 19 "bits" (20 if you use unsigned long). But the better solution is to use a string instead, if the goal is simply to see a binary representation.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok I get it! Is there any way to optimize the code so that i = i*10 won't get that big ?
@eMe no, i is an integer type; jack it up by x10 with each iteration and sooner or later you're going to overflow. So don't do that in the first place, as it says how in the last sentence of this answer: stop using a integral value for storing your bits in the first place (i.e. use a string of digit characters, a bitset, etc.)
@eMe: No. Any way you try to do that, you end up using a decimal digit's worth of space per "bit". What you need is to get away from the whole idea of using an int or long to hold your numeral. That's not what it does (at least not well). You can do something very similar with a string instead.
In my mind if I'm going to use "i" as a string I don't see how it would work. Because you can't multiply and divide strings with numbers can't you ?! I have to read that thing you showed me. I'm sure the answer is already there. Forgive me for being a newb and thanks for ur help! Ok so probably there is a difference between a bitset and a string.
@eMe: Yes, there is. A bitset is made for doing bitwise stuff with. You can examine individual bits and twiddle them to your heart's content. If you want to do that, then it's a better choice. A string is more for if you just want to see your number represented a certain way.
|
3

There is a problem with your code. You actually, don't convert a base 10 number to base 2. You store your number as int or long, that is why it is becoming bigger than the int or long size very quickly. The numbers are kept already in binary in our computer.

If you really want to print the number see std::bitset.

If you want to play the numbers, like setting or clearing bits you have to use some mask. Or, better use std::bitset from the c++ standard library;

2 Comments

Ok I get it! I'll go through that "std::bitset" post, it looks very interesting. Also... is there any way to optimize the code so that i = i*10 won't get that big ?
Please read carefully, The number is already stored binary. You only need printing, rest are operations...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.