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!!!

2. 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.

3. Originally Posted by quzah
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. Originally Posted by Laila Sawyer
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:
Originally Posted by Laila Sawyer
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.

5. Originally Posted by quzah
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. 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

7. 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.

8. 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

9. Originally Posted by Laila Sawyer
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.

10. 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.

11. 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. Originally Posted by Laila Sawyer
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;`