Hey everyone another stupid question.

Is ther a function that allows me to count in binary...cause i been racking my brain for about 2 hours and i cant seem to get the logic rite.

Printable View

- 01-24-2003dustydcounting in binary
Hey everyone another stupid question.

Is ther a function that allows me to count in binary...cause i been racking my brain for about 2 hours and i cant seem to get the logic rite. - 01-24-2003Shiro
What do you mean with counting in binary?

0 = 0

1 = 1

2 = 10

3 = 11

4 = 100

5 = 101

etc.

1 = 2^0 = 1

100 = 2^2 = 4

101 = 2^0 + 2^2 = 5 - 01-24-2003OneStiffRod
here's how binary works,

00 = 0

01 = 1

10 = 2

11 = 3

we add from right-to-left, if we have 00 we add by 1 which makes 01, and if we add another 1 we cannot go past a 0 or 1 value so we shift the 1 to the left which makes it 10.

You can tell how many possible combinations in a binary set, such as 1 byte can hold by doing POWERS.

1 byte is 8 bits.

0000 0000

2^8 (2 to the 8th power) = 256 so the values are between 0 - 255 or if signed the values are -127 - 128. - 01-24-2003dustyd
when i say counting in binary, i just can't figure how to apply it to c...i know how to count in binary but when i apply it to c like 101 + 110 will come out 212 insted of 1011...so i was wondering if ther is a function or a theory on how i can make it so that when i add it adds in binary

- 01-24-2003Shiro
In C++ if you calculate 101 + 110 you get 212, that is correct. It would be incorrect if it were 1011, because C++ uses base 10 (decimal), not base 2 (binary).

To calculate 101 + 110 in binary, you would first have to convert this from binary to decimal and then calculate the sum. Such like:

int sum;

sum = to_decimal (101) + to_decimal (110); - 01-24-2003PJYelton
What do you mean by apply it to c? Do you mean the language C?

I don't think there is a function that adds binary like the way you want to, but its easy enough to create one yourself. One way would be to convert the number to decimal, add then, then convert back to binary.

To do it arithmetic wise, do like in the old days of math, put the numbers on top of each other, and add from right to left carrying over.

Code:`101`

+111

-------

- 01-24-2003OneStiffRod
<iomanip.h>

has functions for printing out Hex and Decimal formats - not sure - but maybe it also has a binary format function? - 01-24-2003Polymorphic OOP
In C and C++ you can only directly express literals in decimal, octal, and hex (and ascii). Not binary.

It's stored as binary and calculations are in binary because that's the only thing the computer can understand. - 01-25-2003DougDbug
EVERYTHING in the computer is stored in BINARY! So, if you are adding 3+3, the computer is adding 11 (binary) to 11 (binary) and getting 110 (binary). So somewhere inside the computer's RAM and/or CPU register, you would find a memory location where the three least-significant bits are 110.

The compiler and operating system are “kind-enough” to convert all these numbers back to decimal for you. And, I was surprised to learn recently that you can’t include binary in your source code as you can hex. (You can write decimal 10 as “10” or as hex “0x0A”)

So, the key point here is that any conversion ONLY happens during input or output.

I don’t know any more than OneStiffRod about the output conversion. I sort-of know of a function to convert a string in base-x to an integer [similar to atoi() or atoh() ]. But I don’t have an example at-hand. - 01-25-2003Polymorphic OOP
You can also express ten as 012

- 01-25-2003Sebastiani
Here's one way. This example prints the bits of a signed integer in reverse...I'll leave it to you to make improvements that may be necessary...:)

Code:`int`

set_bit(int * data, int bit)

{

return *data |= 1 << bit;

}

int

clear_bit(int * data, int bit)

{

return *data &= ~(1 << bit);

}

bool

is_set(int data, int bit)

{

return data & (1 << bit);

}

char *

binary(char buff[33], int x)

{

buff[32] = 0;

for(int y = 0; y < 32; ++y )

{

buff[y] = is_set( x, y ) ? '1' : '0';

}

return buff;

}

int

integer(char buff[33])

{

int value = 0;

for(int y = 0; y < 32; ++y )

{

if(buff[y] == '1')

{

set_bit(&value, y);

}

}

return value;

}

int

main(){

char buff[33];

char input[50];

int byte4, bit;

while(byte4 != 0){

cout << "Enter a value" << endl;

byte4 = atoi(fgets(input, 50, stdin));

cout << binary(buff, byte4) << " = ";

cout << integer(buff) << endl;

cout << "Enter a bit to set (1-32)" << endl;

bit = atoi(fgets(input, 50, stdin));

if(bit && bit < 33)

{

--bit;

cout << binary(buff, set_bit(&byte4, bit) ) << " = ";

cout << integer(buff) << endl;

} else cout << "Out of range..." << endl;

cout << "Enter a bit to clear (1-32)" << endl;

bit = atoi(fgets(input, 50, stdin));

if(bit && bit < 33)

{

--bit;

cout << binary(buff, clear_bit(&byte4, bit) ) << " = ";

cout << integer(buff) << endl;

} else cout << "Out of range..." << endl;

}

return 0;

}

- 01-25-2003DougDbug
Here are some functions that appear to handle any numeric base:

For input:

strtoul() ANSI (string to long)

strtod() ANSI (string to double)

For output:

ultoa() NON-ANSI Microsoft (unsigned long to string) - 01-25-2003Cela
>>Here are some functions that appear to handle any numeric base:

Up to 36 of course. :-) - 01-25-2003joshdick
I'd create a class to deal with binary numbers. Such a class could contain utility functions to convert integers to binary and vice versa. The binary representation of the number could be stored as a string and an int variable could be used for calculations. But, such a class could be somewhat time consuming (at least for me), so I'd only suggest this if you're really going to be working with binary numbers for a considerable amount of time. Just a suggestion.

Hmmm... Ya know what? I think coding a binary class would be fun. Maybe that'll be my next project. Well, if you see questions in the near future about a binary class, don't be suprised.