The Student Room Group

(Delphi) I want to store an integer in base-2 (not how it normally is, please read)

I know that integers are normally stored in base 2 (binary), but when you print it to the screen, it comes out in base 10 (decimal). Basically, I want to make it so I can print it to the screen (in binary) and for example add 1 to it and it says in binary form (so, for example, 0001 goes to 0010 and not 0002 :smile:

I hope that's explained good enough :p:

If this is possible, I imagine it would be by declaring your variable as a binary variable or something?

Please tell me :smile: Thank you :smile:
Reply 1
Integers are stored in base 2 automatically. That's the only base computers work in.

I assume you mean that you want the number to be displayed in base 2, or you want to enter raw base 2 numbers to do maths with it?
Reply 2
laser
Integers are stored in base 2 automatically. That's the only base computers work in.

I assume you mean that you want the number to be displayed in base 2, or you want to enter raw base 2 numbers to do maths with it?

Oh yeah, sorry I wasn't clear, I want it to be displayed in base 2.
Reply 3
OK here is an example to do what you describe. It is written in C++ but it's very simple syntax and hopefully you can understand. Feel free to ask if you don't though.


int Test = 10;

while(Test > 0)
{
std::cout << ((Test % 2) > 0);
Test /= 2;
}


So here I have an integer variable, Test, holding the number 10.

Now in the loop is where the magic happens. I am performing what's called "modulus" (That's the % operator you see there). This will essentially return the remainder of diving a number by another number. In this case it will return the remainder when Test is divided by two. The entire statement ((Test % 2) > 0) is testing to see if this remainder is bigger than zero i.e. If Test goes into 2 exactly with no remainders which means that the current bit in the binary number should be set to one (As binary is base 2 so if there are remainders it means that Test will not fit completely into any base 2 number). The result of that statement is output to the screen.

I then divide Test once by two and the loop continues until Test is reduced to zero. std::cout is simply "display this to the screen". I hope this helps you.

Oh note that this outputs binary which should be read from left to right. So the left most bit is 1, then 2 and so on.

I realise my explanation might be confusing. I was harder than I thought it would be to put it into written word so feel free to say "WTF" at me and I'll try to put it more succinctly.
Reply 4
Dom152

int Test = 10;

while(Test > 0)
{
std::cout << ((Test % 2) > 0);
Test /= 2;
}


Ah that's pretty sweet, that's a lot simpler that what I was slowly putting together. This is what it would look like in Delphi though (with a few temporary variables [a and b] and with a string variable, s):



B := 10; {or user input}
while (B > 0) do
begin
A := B MOD 2;
B := B DIV 2;
write(a); {this would write them out in reverse order, but I can deal with that}
end;

Reply 5
Out of interest what where you thinking of doing?

Just so you know you shouldn't be outputting A itself. You need to output the result of (A > 0) (As a 1 for true or a 0 for false). Outputting just A would just display the remainder which isn't what you want.

And yeah it shouldn't be too difficult to get it to output the other way round. Storing the results in an array and writing it out in reverse order springs to mind though there's probably a better way!
Reply 6
Dom152
Out of interest what where you thinking of doing?

Just so you know you shouldn't be outputting A itself. You need to output the result of (A > 0) (As a 1 for true or a 0 for false). Outputting just A would just display the remainder which isn't what you want.

And yeah it shouldn't be too difficult to get it to output the other way round. Storing the results in an array and writing it out in reverse order springs to mind though there's probably a better way!

Yeah, I did the array. Although I don't see what's wrong with outputting the remainder. If you divide by 2 the remainder is either a 1 or a 0, and if the remainder is 1 the result of (A > 0) will also be 1, and if the remainder is 0 the result of (A > 0) will also be 0, won't it?
Reply 7
Tla
Yeah, I did the array. Although I don't see what's wrong with outputting the remainder. If you divide by 2 the remainder is either a 1 or a 0, and if the remainder is 1 the result of (A > 0) will also be 1, and if the remainder is 0 the result of (A > 0) will also be 0, won't it?


Hehe, good point. Was having a dumb moment there :biggrin:
Reply 8
Dom152
Hehe, good point. Was having a dumb moment there :biggrin:

Hehe :p: Cheers for help :smile:

Latest

Trending

Trending