Thread: Beginning C programming class, decimal to binary conversion code!! Please HELP!!!!?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Beginning C programming class, decimal to binary conversion code!! Please HELP!!!!?

    I am one week into a beginning C programming course and so far we have reached if statements, and nothing further (no while loops, just basic defining of variables, using relational operators, the if statements, etc.) So I need some help fixing my BASIC coding ...

    Basically, the program needs to ask the user to input an integer, convert that integer to binary but only for the last 4 bits, and print those 4 bits. Then, it has to do this AGAIN for a second integer given by the user. (There is more to this program but this is the first part, and I need to fix it).

    Basically, my code works for bigger integers that are already 4 or more bits in length when converted to binary. BUT integers that have binary lengths of LESS than 4 bits are printing wrong, because instead of 0's put to the left of their value to equal 4 bits of length, my code prints 1's. I'm using multiple if statements, one for each bit, and I need each bit to be given its own variable because I will be using these variables for something else later on.

    Anyway, here is my code:
    Code:
    #include <stdio.h> /** Include the stdio.h file to allow for printf() to be used*/
    
    /** Project 1 for CS151 by NS
    */
    
    int main (void) {
    	int x; // This is the FIRST integer that the user enters.
    	int y; // This is the SECOND integer that the user enters.
    	int newx; // This is the ever-changing value of x as we break it down to find the zero or one remainders.
    	int newy; // This is the ever-changing value of x as we break it down to find the zero or one remainders.
    	int a1, b1, c1, d1; // These are the values of the last 4 bits of the FIRST integer given by the user. Currently they are in reverse order.
    	int a2, b2, c2, d2; // These are the values of the last 4 bits of the SECOND integer given by the user. Currently they are in reverse order.
    	
    	puts("Please enter an integer. (Integers are whole numbers - numbers without fractions or decimals, so please take note.): \n");
    	scanf("%i", &x);
    	
    /** Convert the FIRST integer (x) into binary, printing only the last four bits. 
    */
    	if (x/2 >= 1) {
    		a1 = x%2;
    		newx = x/2;
    		}
    		else {
    		a1 = 1;
    		}
    		
    	if (newx/2 >= 1) {
    		b1 = newx%2;
    		newx = newx/2;
    		}
    		else {
    		b1 = 1;
    		}
    		
    		if (newx/2 >= 1) {
    		c1 = newx%2;
    		newx = newx/2;
    		}
    		else {
    		c1 = 1;
    		}
    		
    		if (newx/2 >= 1) {
    		d1 = newx%2;
    		newx = newx/2;
    		}
    		else {
    		d1 = 1;
    		}
    		
    	/**
    	Print the results of the decimal to binary conversion (last four bits only):
    	*/
    	
    		printf("The last four bits of %i are %d%d%d%d. \n", x, d1, c1, b1, a1);
    		puts("Now, please enter a second integer. \n");
    		scanf("%i", &y);
    		
    	/** Convert the SECOND integer (y) into binary, printing only the last four bits. 
    	*/
    		if (y/2 >= 1) {
    		a2 = y%2;
    		newy = y/2;
    		}
    		else {
    		a2 = 1;
    		}
    		
    		if (newy/2 >= 1) {
    		b2 = newy%2;
    		newy = newy/2;
    		}
    		else {
    		b2 = 1;
    		}
    		
    		if (newy/2 >= 1) {
    		c2 = newy%2;
    		newy = newy/2;
    		}
    		else {
    		c2 = 1;
    		}
    		
    		if (newy/2 >= 1) {
    		d2 = newy%2;
    		newy = newy/2;
    		}
    		else {
    		d2 = 1;
    		}
    	
    		printf("The last four bits of %i are %d%d%d%d. \n", y, d2, c2, b2, a2);
    		
    		return (0);
    		
    	}
    So how do I fix this so that values less than 4 bits add the appropriate # of 0's to the left to make it '4' bits? Make it all one if statement? If so, I'm not exactly 100% clear on the rules of elseif, so please be very specific, or even modify the code itself for me.

    Also, I realize there is more than one way to go about this. One option someone suggested to me (without expanding on) was to go about it by seeing if x can subtract 8, then 4, then 2, then 1, etc. Our homework hinted that this was a possibility, but it also suggested the method I used, which is utilizing / and % operators. So if you think one method or the other is better, please be SPECIFIC in how to implement them!

    Also, this assignment is due at midnight tonight (Pacific time), so please respond ASAP!! thank you!!!
    Last edited by Laila Sawyer; 07-04-2011 at 11:30 PM. Reason: Inlining the code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No one wants to go download a file instead of just reading it in the forum. You also aren't actually saying what you are having problems with, which leads me to believe that you didn't read the homework policy.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by quzah View Post
    No one wants to go download a file instead of just reading it in the forum. You also aren't actually saying what you are having problems with, which leads me to believe that you didn't read the homework policy.


    Quzah.
    Thanks to whoever fixed the inlining code issue for me, although this would not have required you to download a file seeing as it would have opened right in the browser. Either way, my mistake.

    And Quzah, I *did* specify what I am having a problem with -- how to modify my code so that it won't have problems with integers under 8 (since integers under 8 are those whose binary values are less than 4 bits). If you read my post more carefully you'd see that. I'd appreciate your patience seeing as I am completely new to programming, not a seasoned expert like any of you.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laila Sawyer View Post
    how to modify my code so that it won't have problems with integers under 8 (since integers under 8 are those whose binary values are less than 4 bits). If you read my post more carefully you'd see that. I'd appreciate your patience seeing as I am completely new to programming, not a seasoned expert like any of you.
    So in other words, you don't have anything:
    Quote Originally Posted by Laila Sawyer View Post
    Basically, the program needs to ask the user to input an integer, convert that integer to binary but only for the last 4 bits, and print those 4 bits.
    You are only trying to mask off the last 4 bits anyway, so since that's what you haven't figured out how to do, you have nothing at all.

    if only there was some way to check to see if a variable is < a number, say 8 for example...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by quzah View Post
    So in other words, you don't have anything:You are only trying to mask off the last 4 bits anyway, so since that's what you haven't figured out how to do, you have nothing at all.

    if only there was some way to check to see if a variable is < a number, say 8 for example...


    Quzah.
    The code does work for larger values, so I don't see how I have "nothing." This method was one of two methods hinted at by my professor, so I know that if I could modify my current code in some way it could work.

    The other method is the one you seem to be hinting at, but if I could figure it out myself I wouldn't be posting on this forum, asking for help. I realize that you're trying to get me to think for myself rather than just handing me the answers, but if you could be more specific that would be great, because I'm not quite connecting the dots when it comes to some of the logic behind problem. Again, I'm completely new to this, so please bear with me and please be considerate.

    Thanks.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Alright, so let's clean up that code and pair it down to just what we need to worry about:
    Code:
    #include <stdio.h> /** Include the stdio.h file to allow for printf() to be used*/
    
    /** Project 1 for CS151 by NS
    */
    
    int main (void) {
    	int x; // This is the FIRST integer that the user enters.
    	int newx; // This is the ever-changing value of x as we break it down to find the zero or one remainders.
    	int a1, b1, c1, d1; // These are the values of the last 4 bits of the FIRST integer given by the user. Currently they are in reverse order.
    	
    	puts("Please enter an integer. (Integers are whole numbers - numbers without fractions or decimals, so please take note.): \n");
    	scanf("%i", &x);
    	
    /** Convert the FIRST integer (x) into binary, printing only the last four bits. 
    */
    	if (x/2 >= 1) {
    		a1 = x%2;
    		newx = x/2;
    	}else{
    		a1 = 1;
    	}
    		
    	if (newx/2 >= 1) {
    		b1 = newx%2;
    		newx = newx/2;
    	}else{
    		b1 = 1;
    	}
    		
    	if (newx/2 >= 1) {
    		c1 = newx%2;
    		newx = newx/2;
    	}else {
    		c1 = 1;
    	}
    		
    	if (newx/2 >= 1) {
    		d1 = newx%2;
    		newx = newx/2;
    	}
    	else {
    		d1 = 1;
    	}
    		
    	/**
    	Print the results of the decimal to binary conversion (last four bits only):
    	*/
    	
    	printf("The last four bits of %i are %d%d%d%d. \n", x, d1, c1, b1, a1);
    	
    	getchar();
    	getchar();
    	return (0);
    		
    }
    Now your problem shows up when you have a value less than 8 because instead of inserting a leading 0 your code places a 1 there. So looking at the evaluation where the 1 of 4 bits are determined (in red), what do you think you could do to ensure if the integer entered was less than " < " 8 the first bit stored would be 0?

    EDIT : Fixed where red was
    Last edited by AndrewHunter; 07-05-2011 at 12:18 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you checking to see if the result is greater than or equal to 1? Just do 4 divisions regardless of what's going on, or, initialize your variables to 0, and only do as much division as you need.


    Quzah.
    Last edited by quzah; 07-05-2011 at 12:22 AM. Reason: clarity
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Andrew - thanks for your helpful reply, I've stared and stared at my code and tried to figure out the answer to that same question you asked a million times already myself. I wish I knew! This is practically the first code I've *ever* written so I really am clueless when it comes to what I might be able to utilize to my advantage for this problem... and I think I still haven't quite yet gotten the logical thinking behind it all down yet... so if you could be a little more direct with your hints that'd be great. Like I said, I'm a complete newbie so what might seem obvious to you is not so much to me...

    ... and if you could reply within the next 30 mins (before I have to turn this thing in!) that'd be great too!

    Quzah - take a look at the section under the red line of text here (Step 8) and you might have a better idea of why I used >=1 (I could have also just used >0 I suppose): CS151 Binary -> Decimal
    Last edited by Laila Sawyer; 07-05-2011 at 12:37 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laila Sawyer View Post
    Quzah - take a look at the section under the red line of text here and you might have a better idea of why I used >=1 (I could have also just used >0 I suppose): Untitled Document
    Why bother with either? mod divide mod divide mod divide mod and you are done. No if checks needed. Just pay attention to the math itself (the link you provided, the spot you pointed to) and stop over complicating it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so d1 is where you are storing your leading bit. You figure out the value for your d1 here:
    Code:
    if (newx/2 >= 1) {
    	d1 = newx%2;
    	newx = newx/2;
    }else {
    	d1 = 1;
    }
    Your problem is that when your integer entered is less than 8, you assign 1 vice 0 to the leading bit. Can you see where that happens? (Hint: It may be a different color)

    So for a quick and dirty solution you would want to put a special case in if the initial value was < 8 as has been suggested by quzah. A standard if-else if-else chain may look like:
    Code:
    if(*something*){
        *do this*
    }else if(*something else*){
       *do that*
    }else{
       *do whatever*
    }
    BTW- quzah has been giving you the answer, and I am quite sure he understands how to convert decimal to binary.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    You know what, you're completely right. I've been fixing it since seeing your edit on you previous post, and it works! Gahh, this is the part where I bang my head against the wall.. haha. Thank you Quzah. I think I just got stuck on the fact that we had just learned if statements last week and I assumed this meant we were suppose to use them in this assignment. THANK YOU!!

    If you don't mind, do you know why this 'NOT' bitwise operator might not be working?

    d3 = ~d1;
    c3 = ~c1;
    b3 = ~b1;
    a3 = ~a1;
    printf("NOT operation on the first input: %d%d%d%d \n", d3, c3, b3, a3); // NOT operation

    Sorry I haven't figured out how to inline my code or whatever, but I need to turn this assignment in within 10 mins so I'm kinda in a rush. Anyway, everything in my overall code is working fine now, except for that NOT operator... it prints out: "NOT operation on the first input: -1-2-2-1"

    THANK YOU again Quzah and if there's any thing you can tell me about this last little error that'd be great! I really am appreciative!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laila Sawyer View Post
    If you don't mind, do you know why this 'NOT' bitwise operator might not be working?

    d3 = ~d1;
    c3 = ~c1;
    b3 = ~b1;
    a3 = ~a1;
    printf("NOT operation on the first input: %d%d%d%d \n", d3, c3, b3, a3); // NOT operation
    It's not a NOT operator. It's a "flip all the bits the other way" operator. So what you used to have (assuming 8 bits for the sake of simplicity) was:
    Code:
    d1 = 00000001
    d3 = ~00000001;
    d3 = 11111110;
    If you want !, then use !:
    Code:
    d3 = ! d1;
    See also: http://faq.cprogramming.com/cgi-bin/...&id=1043284351


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  2. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  3. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM