Thread: Help with a bit manipulation program.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Help with a bit manipulation program.

    The object of my program is to repeatedly read in 8-bit binary numbers (bytes) from the keyboard, keeping a running sum of the numbers. The program will terminate when the user enters the character 'q' (for quit). I have no idea where to start as my professor sucks at teaching.

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    anyone? anything?

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    hmm, I'd suggest to start by reading the chapters he told you to read.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    We dont have a book, he teaches with his power point slides, and they are worthless, he just throws random assignments at us.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    1. Don't bump your own threads.
    2. No, we won't do your homework for you.
    3. Educating is a two step process. Your teacher may "suck" at teaching, but apparently you haven't done your part to learn anything.

    Show us your start, and then ask specific questions.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    alright im sorry...
    we pretty much input an unsigned value and we need to convert it to binary...

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'll start by telling you that
    Code:
    unsigned char MyUnsigned8BitVariable
    will get you your 8 bit number.
    Code:
    unsigned char
    is exactly one byte. I hope you know how to do input and output. Other than that, I hope you can figure out how to write a function to convert decimal to binary. If you can't figure it out then show me the code that you have and I'll actually try to help you rather than complaining that you're trying to get help on your assignment.

    You asked for a starting point and I gave you one. Put your code up so that I can help you.
    Don't quote me on that... ...seriously

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >will get you your 8 bit number
    It will? Oh, I see. Clearly it's an 8-bit number because you called it "MyUnsigned8BitVariable" and C compilers are smart enough to know that you want 8 bits and give you exactly 8 bits.

    Now, if you said "will get you your 8 bit number on most modern machines", I would have passed you by with a smile because you were clever enough to cover your butt from nitpickers. char is not required to be 8 bits. Why do you think CHAR_BIT exists?
    My best code is written with the delete key.

  9. #9
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    That wasn't a response that I had planned for.

    In 8 bit game programming, you always use and unsigned char to represent an 8 bit pixel. There may be an exception on a small amount of computers, but using an unsigned char is still very reliable.

    I know that more than likely, an unsigned char will be 8 bits on his computer and since he is having trouble with this assignment, I didn't want to worry him with the fact that there is a very small chance that this might be false. If you are concerned with the number of bytes an unsigned char is on your computer, this program will tell you:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("The size of an unsigned char on this machine is %d byte(s).\n",
    		   sizeof(unsigned char));
    	return 0;
    }
    If this tells you that an unsigned char is more than 1 byte on your computer then I am deeply sorry for not considering that possibility. IF IT IS 1 BYTE THEN DON'T CRY LIKE A LITTLE BABY EVERY TIME YOU SEE A TINY INSIGNIFICANT ERROR. TRY HELPING PEOPLE RATHER THAN COMPLAINING ABOUT PEOPLE WHO ARE TRYING TO HELP.

    I see you have 7,714 posts right now. I hope that at least one of them actually helped someone.
    Don't quote me on that... ...seriously

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Actually, sizeof(char) will always be 1 so that program wouldn't give you anything useful.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Not on machines where a char is 2 bytes.

    I also forgot to mention that if you are like Prelude and are worried that a char is actually 2 bytes, you can always remove any extra bits by simply doing this:
    Code:
    char character;
    // change all the extra bits to zero
    character = character & 255;
    Prelude didn't mention that. Maybe she didn't know or was just to busy complaining that she forgot to mention.
    Don't quote me on that... ...seriously

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Brad0407
    Not on machines where a char is 2 bytes.
    It is now you passing disinformation; you could stand to learn a little more before tyring not to confuse others.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Brad0407
    Not on machines where a char is 2 bytes.

    I also forgot to mention that if you are like Prelude and are worried that a char is actually 2 bytes, you can always remove any extra bits by simply doing this:
    Code:
    char character;
    // change all the extra bits to zero
    character = character & 255;
    Prelude didn't mention that. Maybe she didn't know or was just to busy complaining that she forgot to mention.
    Stop crying about your imagined hurt feelings and pay attention. We, the people who know what the hell they're talking about, tend to state facts pulled from the standard. The standard is what defines the C language.

    You come in and say "A byte is 8 bits!", as a flat out truth. Well, I hate to tell you this* but if you learned anything from grade school tests it's that "If any part of a statement is false, the whole thing is false."

    As such, your statement is false. Bytes are not 8 bits. They're CHAR_BIT bits. You see, the C language doesn't care where it's compiled. It does know however, that the number of bits in a byte is CHAR_BIT. So while what you do on your machine may be correct in some cases, when I say "A byte is CHAR_BIT bits in size!", I am correct in all cases.

    The point is, if you're going to try and teach people, do it correctly. So stop crying like a wuss and be surprised that you with your supposed "mad skillz" have actually learned something today.


    Quzah.
    *Actually, I don't hate it, but it makes for a better sentence than: "You're such a ........ing idiot."
    Last edited by quzah; 10-24-2005 at 05:09 PM. Reason: I'd like to thank my crack editing staff for the quality of this post.
    Hope is the first step on the road to disappointment.

  14. #14
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    When the **** is a byte not 8 bits?

    A bit is a bit
    A nibble is 4 bits
    A byte is 8 bits

    Did they not teach you that when you learned the standard?
    If the standard of a byte is not 8 bits then the guy that is writing his ****ing program is going to have a **** of a time trying to write a program where 8 bits is 1 ****ing byte. And yes I have run this program on a computer with 2 bytes for characters and the result came back as 2 bytes.

    With that anger out, I'd like to say that I figured out how to work out the bottom line of signature, quzah, but I don't know how to display it. lol.

    I still think that using an unsigned char is the best way to go and I would do it myself.

    To think that all this started because I was thinking of helping torqueywrench and not about pointing out every single flaw. I wasn't "covering my butt from nitpickers".

    I did learn something today, quzah, that did surprise me. You know what it is? The people that are smart enough to help nitpick the people doing their best to help. I also learned that these forums are started with good intentions but end up being ruined by people that have no social life because all they do is insult others.
    Don't quote me on that... ...seriously

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Brad0407
    When the **** is a byte not 8 bits?
    From http://en.wikipedia.org/wiki/Byte:
    The word "byte" has several meanings, all closely related:

    1. A contiguous sequence of a fixed number of bits. On modern computers, an eight-bit byte or octet is by far the most common. This was not always the case. Certain older models have used six-, seven-, or nine-bit bytes - for instance on the 36-bit architecture of the PDP-10. Another example of a non eight-bit sequence is the 12-bit slab of the NCR-315. A byte is always atomic on the system, meaning that it is the smallest addressable unit. An eight-bit byte can hold 256 possible values (28 = 256) -- enough to store an unsigned integer ranging from 0 to 255, a signed integer from -128 to 127, or a character of a seven-bit (such as ASCII) or eight-bit character encoding.
    A simple google search would have given you the answer.

    And I'd like to know where you came across this computer that returned 2 for sizeof(char) and the details of it. I think your memory is mistaken.
    Last edited by itsme86; 10-24-2005 at 05:41 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM