i am new to C and i am trying to learn more as i am taking a basic class. we started out with basic programs and now we are making a tic tac toe board. i was wondering what a 8 bit binary counter would look like. i know the formula but dont know how you would apply it in C

Recommended Answers

All 16 Replies

the formula that i know is

10110101= 1( 2^7) + 0( 2^6) +1( 2^5) +1( 2^4) +0( 2^3) +1( 2^2) +0( 2^1) +1( 2^0)

yeah like the code u would write. like i would like to see it. i have no idea how u would bring each bit to the formula and stuff like that

does anyone know???

write it yourself, at least show some effort and you will get better help.

does anyone know???

Did you click in the example link I posted?

i clicked the example link. i still dont kno how to finish it i am sure it is something very easy. can someone help me if we have this code what would i need to add to get it to be a 8 bit counter... ali is a big help... also could someone explain what they did. please help

#include <stdio.h>

/* Convert the text representation of a binary number into an integral value. */

unsigned int btou(const char *bit)
{
unsigned int value = 0;
while ( *bit != NULL ) /* loop through all of the text representation */
{
value *= 2; /* double the previous result */
value += *bit - '0'; /* add current bit (0 or 1 from '0' or '1')*/ ++bit; /* go on to the next bit */
}
return value;
}
int main ( void )
{ static const char *binary[] =
{ "0000","0001","0010","0011","0100","0101","0110","0111", "1000","1001","1010","1011","1100","1101","1110","1111", "1010010110100101", "11011110101011011011111011101111",
};
size_t i;
for ( i = 0; i < sizeof binary / sizeof *binary; ++i )
{
unsigned int value = btou(binary);
printf("btou(\"%s\") = 0x%X = %u\n", binary, value, value);
}
return 0;
} /*
my outputbtou("0000") = 0x0 = 0
btou("0001") = 0x1 = 1
btou("0010") = 0x2 = 2
btou("0011") = 0x3 = 3
btou("0100") = 0x4 = 4
btou("0101") = 0x5 = 5
btou("0110") = 0x6 = 6
btou("0111") = 0x7 = 7
btou("1000") = 0x8 = 8
btou("1001") = 0x9 = 9
btou("1010") = 0xA = 10
btou("1011") = 0xB = 11
btou("1100") = 0xC = 12
btou("1101") = 0xD = 13
btou("1110") = 0xE = 14
btou("1111") = 0xF = 15
btou("1010010110100101") = 0xA5A5 = 42405
btou("11011110101011011011111011101111") = 0xDEADBEEF = 3735928559*/

#if 0
/* of course, if you can use standard library functions, it would be simpler yet */
#include <stdlib.h>
unsigned int btou(const char *bit)
{
return strtoul(bit, NULL, 2);
}
#endif

i kno that ou putan binarry [] that is the only change i can really see.
{ static const char *binary[8] =
{ "0000","0001","0010","0011","0100","0101","0110","0111", "1000","1001","1010","1011","1100","1101","1110","1111", "1010010110100101", "11011110101011011011111011101111",

also what about the i all over the place
size_t i;
for ( i = 0; i < sizeof binary / sizeof *binary; ++i )

/* Binary to Decimal **********************************************************/
me and an old friend worked on this freshman year but i cant have the bool and need it in


#include "stdafx.h"
#include <stdio.h>


instead of


#include <iostream>
#include <cstdio>
using namespace std;


bool binary[8]; // unused, but if you need to store it, it will be in here
char str[8];    // input string
int result;     // integer result
int power = 1;  // power of 2. This is needed because of (2 ^ i) problem (see below)


int main(void)
{
do
{
cout << "Please input an 8 bit binary number or type q to quit:";
cin >> str;


result = 0; // reset to 0
power = 1;  // first power = 2 ^ 0 = 1


/* convert it */
for(int i = 0; i < 8; i++)
{
if(str == '1')
{
binary = 1;// make binary number
result += power; // modify result
}
power *= 2; /* id like to use 2 ^ i instead of power but it
DOESNT give 1, 2, 4, 8, 16 ect... */
}


/* output */
cout << "\nThe binary number " << str << " = " << result << "\n";
} while (str[0] != 'q');   // do - while until "q" is typed
return 0;
}

Congratulations, 3 posts in a row without using code-tags.

what do u mean code tags????

What on earth do you need a binary counter for in a tic tac toe game? Computers store everything in 8-bit binary. How you refer to the number doesn't make a difference, other than maybe speed.

What on earth do you need a binary counter for in a tic tac toe game? Computers store everything in 8-bit binary. How you refer to the number doesn't make a difference, other than maybe speed.

They store it in binary yes, but we left the 8-bit addressing range many decades ago - even toys, and appliances have more than that! In the general computer market 32-bit is pretty standard, with a switch towards 64 becoming standard, soon.

Sorry, I was talking about the lowest subdivision of data programmers can work with: the byte. Yes, addresses certainly require more than 8 bits.

They store it in binary yes, but we left the 8-bit addressing range many decades ago - even toys, and appliances have more than that! In the general computer market 32-bit is pretty standard, with a switch towards 64 becoming standard, soon.

It seems like u're good in C=)

So i'm looking for the same program, but without using library just stdio.h

can u help me? maybe just recreate some code?
that code below from the girl, i can't get it, where are input and output?

i need to get binary number from a user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.