I'm new to this board but not to C .
I'm a geek who loves to mess with binary.
I was part of a QBasic community at network54 but everybody started fighting when FreeBASIC came out. I switched to the reliablity of C to share my knowledge with the world.
I currently use Ubuntu Linux.
C is the so cool because you can compile
on many different systems.
I wrote an interesting program recently.
I then realized that I didn't have any good
place to post it. Even the people I talk to most of the time are like
"programming is hard! I don't want to understand it I just want it to work!"
I cannot stand people who don't have extreme patience and love learning.
I hope to find some intelligent people here.
Below is my new program. It will give you an idea of how much experience I have.
Code:
/*
This is THE customizable binary counter!
It allows you to set the number of bits.
It allows you to customize it to use
any ASCII codes to display for zero and one.
This uses the incredible XOR flip as a means of
switching between the two constants.
If you try to set both zero and one to the same ASCII code,it
will loop for all eternity!
I used fwrite to output the bits. This means that we don't need
to use printf which is slow cause of parsing the format.
I do however use a printf for a newline. This is for compatibility
with different systems which use different bytes for newline.
In spite of the extra compatibility and customization,this program
which I have been working on for years only gets faster!
I'm so proud of it in fact,that I've commented it!
gcc -ansi -Wall -s 0.c -o 0
*/
#include<stdio.h>
#define bits 0x8 /*number of bits used*/
#define zero 0x30 /*ASCII code of zero*/
#define one 0x31 /*ASCII code of one*/
#define flip zero^one /*used only to flip the bit*/
int main()
{
unsigned int c;
unsigned char s[bits+1];
for(c=0;c<=bits;c++){s[c]=zero;} /*set all bits to zero*/
/*
Notice that element 0 is used as sort of a carry.
This was needed to allow the program to pass the machine limits
*/
while(s[0]==zero)
{
fwrite (s+1,sizeof(s[0]),bits,stdout);
printf("\n");
c=bits+1;
do
{
c--;
s[c]^=flip; /*flips the bit!*/
}
while(s[c]==zero); /*if the bit isn't 0,nothing else to do*/
/*
note that the do while loop is used instead of while as
it must unconditionally execute at least once.
*/
}
return 0;
}