I'm new to this board but not to C .

This is a discussion on I'm new to this board but not to C . within the C Programming forums, part of the General Programming Boards category; I'm a geek who loves to mess with binary. I was part of a QBasic community at network54 but everybody ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11

    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;
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    its good but some better indentation would make it a little easier to read. But thats a very minor critisism
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11
    I used to have an indented version. However,I rewrote most of this completely from scratch.
    It doesn't make it easier for me to read so I often don't think about it.

    Other than that,do you understand how it works the way it does?
    It takes some pretty deep knowledge of XOR but is simple seeing as it
    uses only an array and the "c" variable as an index,it's not all that complex.

    I would indent it in a few areas if somebody wished to use it on their site or something.
    All code I write is freely usable for whatever purpose you have in mind.
    I view programming as THE way of sharing things .

    I'd like to hear some feedback on how fast it runs for you.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Quote Originally Posted by honorable_sir View Post
    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.
    That's an interesting choice. I don't think there is ever a reason to avoid using stdout in text mode, though, especially when there are standard output functions that don't parse a format string and wouldn't be as slow as you suggest. puts(), for example.

    It's also a bit odd that you've chosen to use hex constants for characters like '0' and '1'. You've gained nothing from that.

    At least it runs "fast."

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> I cannot stand people who don't have extreme patience and love learning.
    Patience is normally hard to find in people let alone extreme patience, so you may encounter a few people you can't stand here. Most everyone here loves to learn though.

    >> It doesn't make it easier for me to read so I often don't think about it.
    Well, the majority of this board really does like indented code, it's just our thing.

    Welcome to the boards.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by honorable_sir View Post
    reliablity of C
    Oxymoron!

    I cannot stand people who don't have extreme patience and love learning.
    You don't have patience for people who don't have patience? WTF?

    Code:
    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.
    That's what we need, optimization to the point of insanity.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11

    :D

    You don't have patience for people who don't have patience? WTF?
    LOL. I guess I should work on that!
    Well I'll get along with most people.
    So far you guys are friendlier than the FreeBASIC forum members.

    That's an interesting choice. I don't think there is ever a reason to avoid using stdout in text mode, though, especially when there are standard output functions that don't parse a format string and wouldn't be as slow as you suggest. puts(), for example.
    puts has a problem though,it uses 0 terminated strings. I prefer to let my program
    actually output bytes of 0 if you wanted it.
    Supposing somebody redirects the output to a file? It won't always be STDOUT.

    That's what we need, optimization to the point of insanity.
    Yeah,well optimization is good. I used to write
    a lot in assembly using FASM.
    I now care more about portablility.
    Nobody was getting any use out of my Linux programs cause they use Windows!

    Still,I like to make things as fast as possible
    as long as it doesn't sacrifice something else.

    It's also a bit odd that you've chosen to use hex constants for characters like '0' and '1'. You've gained nothing from that.
    Yes, it's a "bit" odd. And I've lost nothing from it either. I always use hex constants as
    I am actually better at reading it.
    Remember,it's supposed to be customizable.
    What if somebody wants other weird values like 0xED and 0x4F ?

    I really don't mind if you change it to suit your needs. Everybody has personal preference.

    BTW,I can count in other bases if needed.
    I used to even do decimal power of two programs that could do thousands of digits.

    Let me know if you're interested in big numbers by use of arrays and not using anything other than the standard library.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Supposing somebody redirects the output to a file? It won't always be STDOUT.
    Makes little or no difference, the only real difference is that puts() isn't binary-safe such as fwrite is.

    Don't want to argue, but get a real distro /opinion

    Yes, it's a "bit" odd. And I've lost nothing from it either. I always use hex constants as
    I am actually better at reading it.
    Remember,it's supposed to be customizable.
    What if somebody wants other weird values like 0xED and 0x4F ?
    In my view you've lost readability (humans work in dec), If you plan on becoming a professional programmer it's not about how you like things, And I would say hex isn't easier to read than decimal. If they wanted 0x4F or something and it can't be expressed in dec, then you can express things in both dec and hex there is really no need to be 'consistent' in the base you work in.
    Last edited by zacs7; 06-24-2007 at 01:33 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > I would indent it in a few areas if somebody wished to use it on their site or something.

    Since you're using Linux, you should know about "indent" which will do your indenting automatically (see "man indent"). But you should be indenting while you write the code, not as an afterthought. Not only is it easier to do this way, since you can enter pairs of matching braces simultaneously so you don't have to locate them later, but it helps you to avoid mistakes like not matching braces, so you can finish your code faster. The proper use of something like "indent" is if someone else has a different preferred indenting style, they can convert the code immediately to it.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  11. #11
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I currently use Ubuntu Linux.
    Great! Another Ubuntu user
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  12. #12
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11

    nice replies

    In my view you've lost readability (humans work in dec), If you plan on becoming a professional programmer it's not about how you like things, And I would say hex isn't easier to read than decimal. If they wanted 0x4F or something and it can't be expressed in dec, then you can express things in both dec and hex there is really no need to be 'consistent' in the base you work in.
    If I told you I wasn't "human:,would you believe me?

    Anyhow,I'm not what you would call "professional". The entire reason I put
    so much effort into a binary counting program is because I PREFER binary as well as any power of two radix.

    I know how to convert binary to decimal and back jbut it takes a lot longer than with hex which makes a much easier conversion.
    Computers use binary and that's why I win
    the argument over which base is truly useful.

    Great! Another Ubuntu user
    Yes! Ubuntu is so sweet

    A note for Prelude.

    You posted more comments than anybody else. Thanks for your hard work.
    However,I don't think you should assume so many things. I am not the best talker.
    I am not good at saying exactly what I mean.

    You are right,good code explains itself.
    However,people had actually requested that
    I comment my code. I do my best. Many people do not automatically know what XOR does! My comments are based on what my
    previous friends understand.

    Back when I started programming in BASIC,
    I did "print 2+2" and was so excited when I got it to display "4" ! I've come a long way
    since then.

    And BTW,your version of the program is
    nearly impossible for me to read.
    It's really weird to include
    stdlib.h and string.h .

    I see no need for struct(which I'll admit to not having learned before).

    The 1 good thing you told me about is
    about fwrite and newlines.

    And yes,I have been working on this for years.
    I mean,not this same C source file,but I have
    written binary counting programs in QBasic,FreeBASIC,Javascript,PHP,
    assembly,and finally C.

    My first versions were rather bad and were
    a lot harder to read. I suggest that you avoid
    reading my posts since you will hate everything about my style.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Computers use binary and that's why I win the argument over which base is truly useful.
    Computers use binary, but people don't. You should be writing your code for people to read, and that's why you lose the argument.

    >I am not good at saying exactly what I mean.
    I can only respond to what you say, not what you really meant. Despite how it might seem sometimes, I'm not a mind reader.

    >My comments are based on what my previous friends understand.
    Write for your audience. I'm not saying you shouldn't comment. I'm saying that you should comment more carefully. I'm sure your previous friends would understand better with improved comments. Remember that you won't always be around to explain in person with diagrams and hand waving.

    >And BTW,your version of the program is nearly impossible for me to read.
    It's not because the code isn't well written. To be perfectly frank, if you've been programming for years and you can't follow my simple code, you're definitely in the right place. We'll help you take your skills to the next level.

    >It's really weird to include stdlib.h and string.h
    Why? I include stdlib.h so that I can create a dynamic array rather than restrict the program like you were doing. I include string.h because using well known functions for common operations makes my intentions easier to understand. A call to memset is more obvious than a hard coded loop.

    >I see no need for struct(which I'll admit to not having learned before).
    There really isn't one except that it groups related information together and makes the code cleaner. A general rule of thumb for designing functions is that if you need more than three parameters, you should refactor operations or merge data.

    >The 1 good thing you told me about is about fwrite and newlines.
    I guess this is where you don't say what you mean. It seems like you're discarding everything else I told you as worthless, which isn't the case.

    >And yes,I have been working on this for years.
    >I mean,not this same C source file
    Then you haven't been working on this for years. It's like me saying that I've been working on my the hello world program for years just because I've re-written it hundreds of times.

    >I suggest that you avoid reading my posts since you will hate everything about my style.
    Didn't you say something in your first post about hating people who don't love learning? Well, you sound like one of those people. You're not interested in the things I'm teaching you and you imply that you have no intention of learning better style. Anyway, if you post your code, people will comment on it. If you can't handle that, don't post. Nothing is more irritating than putting time and effort into a post and then having it largely ignored by an ungrateful recipient.
    My best code is written with the delete key.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    I know how to convert binary to decimal and back jbut it takes a lot longer than with hex which makes a much easier conversion.
    Computers use binary and that's why I win
    the argument over which base is truly useful.
    You are missing the points made. There was no need to prefer 0x8 over 8 since you are expressing the number of bits used, not some specific bit pattern, in which case hexadecimal would be concise and appropriate. There was no need to define zero and one since you could use the more readable '0' and '1', which would be more portable anyway. Telling us that computers use binary is nothing new, you know. As much as you want to "share your knowledge with the world", please remember that we are not ignorant brutes waiting for a higher being to bestow knowledge and intelligence on us.

    And yes,I have been working on this for years.
    I mean,not this same C source file,but I have
    written binary counting programs in QBasic,FreeBASIC,Javascript,PHP,
    assembly,and finally C.
    That's good, but you might want to consider broadening the scope of your study. There is more to binary than just counting, and more to counting than just binary, and more to programming than binary and counting.

    My first versions were rather bad and were
    a lot harder to read. I suggest that you avoid
    reading my posts since you will hate everything about my style.
    You could improve your style with indentation, as was mentioned. At the moment, I suspect many people around here hate your style, simply because control flow gets harder to follow when there is no indentation to provide a visual guide.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11
    You are missing the points made. There was no need to prefer 0x8 over 8 since you are expressing the number of bits used, not some specific bit pattern, in which case hexadecimal would be concise and appropriate. There was no need to define zero and one since you could use the more readable '0' and '1', which would be more portable anyway. Telling us that computers use binary is nothing new, you know. As much as you want to "share your knowledge with the world", please remember that we are not ignorant brutes waiting for a higher being to bestow knowledge and intelligence on us.
    I have a reason for everything.
    I use 0x8 because I am always changing the
    value and having fun watching it.
    I don't want to have to remove the "0x" just
    to add it later. And the whole point of defining zero and one was because I intended somebody to change it so you get
    characters like 'A' and 'Z' for example.

    When you say
    "please remember that we are not ignorant brutes waiting for a higher being to bestow knowledge and intelligence on us."

    you have to realize I come from a community
    where people are that way. I will try to avoid
    stating the obvious facts. When I came here,I honestly thought that nobody would have seen a binary counting program like this.

    And you know why? I've never bet another person who reads or writes binary.
    I am an extreme weirdo. I have text files with
    binary counting that I sometimes stare at for 101 minutes and I'm like "it's so beautiful!"

    And don't say "more readable" .
    You can only read what you have learned.

    An English person can say that English is easier to read than Spanish. Even if they
    know Spanish quite well,they will prefer the language they grew up with.

    Binary is my life. I do everything binary.
    I like to draw pictures at the pixel level and using only black and white. I'll write binary counting on paper and sometimes even my skin!

    I'm sure you realize that without binary,we
    wouldn't be capable of posting on this forum.

    I'm tired of trying to help people read my code.

    Here's what it looks like when I can read it!

    Code:
    #include <stdio.h>
    #define L 0x8
    #define _0 0x30
    #define _1 0x31
    #define F _0^_1
    int main()
    {
    unsigned int c;
    unsigned char s[L+1];
    for(c=0;c<=L;c++){s[c]=_0;}
    while(s[0]==_0)
    {
    fwrite(s+1,1,L,stdout);putchar('\n');
    c=L+1;do{c--;s[c]^=F;}while(s[c]==_0);
    }
    return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 06:02 AM
  2. Constructor problem
    By rebel in forum C++ Programming
    Replies: 22
    Last Post: 01-11-2006, 05:45 AM
  3. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 04:23 AM
  4. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 06:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21