Sorry guys, I just couldn't resist replying to this thread.
>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!"
That describes most of the world.
>I cannot stand people who don't have extreme patience and love learning.
Maybe the people you've talked to just don't care about your program. Did you ever consider that?
>I hope to find some intelligent people here.
So now people who don't have patience and don't love learning (about your program) aren't intelligent?
>It will give you an idea of how much experience I have.
It does indeed.
>This is THE customizable binary counter!
Generally, we don't get people talking about how much experience they have and then proving it with a program that counts from 0 to 255.
>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.
Neither of which are interactive in any way. The only way to "customize" your program is to recompile. I can do that with any program that gives out the source, so how is your program "THE" customizable one?
>This uses the incredible XOR flip as a means of
>switching between the two constants.
It's called a butterfly switch. As I recall, it's hardly incredible and has been a staple optimization of assembly programmers for decades. It's a nice technique to avoid branching instructions in performance critical code, but not nearly as amazing as you're making it out to be.
Not to mention that this isn't a good place to describe the mechanism of your code. What if you find something better and change the code? I bet you won't remember to change the comment. Of course, this program is tiny, so it's hard to miss it...
>If you try to set both zero and one to the same ASCII code,it will loop for all eternity!
Maybe you have so much experience that I don't understand, but wouldn't this be a good place for a sanity check? I mean, if a combination of values is known to choke the program, test for it and don't allow the program to choke.
>I do however use a printf for a newline. This is for compatibility
>with different systems which use different bytes for newline.
You're aware that special character replacements are performed on all text streams, and stdout is a text stream right? fwrite isn't a "binary" function, it's simply unformatted. When you write '\n' to stdout using fwrite, the character is converted to the correct newline combination for your system.
It's also amusing that you give reasons for not using printf, then use it. printf is a heavy weight function, period. Many people do it (even me), but using it to print a newline is technically overkill. You can use puts here, or even putchar.
>In spite of the extra compatibility and customization
What extra compatibility? This is the first I've heard of it. And I still think your customization claims are a little silly.
>this program which I have been working on for years only gets faster!
Wait, you've been working on this for years?! Aroo? Perhaps you meant you've been working with this program for years. Because this is a five minute program, tops. Anyone who knows what a butterfly switch is could write it in no time.
As for being faster, until you remove the I/O, you're not going to go beyond a certain threshold.
>I'm so proud of it in fact,that I've commented it!
That's just weird. It's crap code that I'm embarrassed to show other people that especially needs comments. Code I'm proud of tends to document itself.
>#define bits 0x8 /*number of bits used*/
>#define zero 0x30 /*ASCII code of zero*/
>#define one 0x31 /*ASCII code of one*/
Is there any logical reason you're using hexadecimal notation? I mean, it makes more sense to use a decimal value for the bits and a character constant for the "ASCII" codes. Why? Because people tend to think better in decimal, and your program isn't portable when you assume the character set.
Code:
for(c=0;c<=bits;c++){s[c]=zero;} /*set all bits to zero*/
This line makes me cringe. I'm all for being concise, but be concise with good code rather than by squeezing out all of the whitespace.
Code:
/*
Notice that element 0 is used as sort of a carry.
This was needed to allow the program to pass the machine limits
*/
This is a bad comment. A "sort of carry? Pass "machine limits"? What the hell are you talking about? Element 0 is the most significant bit. When it gets set, you've reached N^bits, and that's one beyond the selected range.
>fwrite (s+1,sizeof(s[0]),bits,stdout);
>printf("\n");
This is where your speed problems are. Your program is I/O bound, so no amount of optimizing the calculations will make it faster than printing the bits.
>/*flips the bit!*/
Another bad comment. It's not difficult to figure out what this line is doing.
Code:
/*
note that the do while loop is used instead of while as
it must unconditionally execute at least once.
*/
Presumably you know what loops to use. I don't think anyone would question you without this comment.
Let's see if we can improve matters a bit:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct binary_value {
unsigned int bits; /* Number of displayable bits */
char clear; /* Display value representing clear bits */
char set; /* Display value representing set bits */
};
int display_all_values ( struct binary_value *binary, FILE *out )
{
int rv = 0;
/* Initialize a butterfly switch */
char flip = binary->clear ^ binary->set;
/* +1 for the most significant bit (no display) */
char *value = calloc ( binary->bits + 1, sizeof *value );
if ( value != NULL ) {
memset ( value, binary->clear, binary->bits + 1 );
while ( value[0] == binary->clear ) {
size_t i = binary->bits + 1;
/* Don't print the most significant bit */
fwrite ( value + 1, sizeof *value, binary->bits, out );
fputc ( '\n', out );
do
value[--i] ^= flip;
while ( value[i] == binary->clear );
}
rv = 1;
}
return rv;
}
int main ( void )
{
/*
Now it's easy to use command line arguments
or interactive input for the customizable values
*/
struct binary_value binary = {8,'0','1'};
if ( !display_all_values ( &binary, stdout ) )
fputs ( "Error displaying all values\n", stderr );
return 0;
}
>I'd like to hear some feedback on how fast it runs for you.
It runs no faster than it takes to print bits+1 characters.