Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am doing a program in C in which I have to read the value of the button in my TM-1638 and sent it to the 7-segment display. I got through the reading part now I am having problems to show the value in the display.
I have converted the value to BCD with this code:
const uint8_t dec[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,0x07,0x7f,0x6f};
if(readbyte > 0)
{
num <<= 4;
num |= readbyte % 10;
readbyte /= 10;
}
senddatabyte(0x0a, dec[num]);
senddatabyte(0x0c, dec[num]);
senddatabyte(0x0e, dec[num &0x0f]);
in which senddatabyte sends the value to the selected adress of the 7 segment display.
It works good when it have to display one digit number but it doesnt work when it displays tens and hundreds.
For larger numbers, you have to use a loop. Loop while the number is larger than or equal to 10. Take the number modulus 10 to get the least significant digit, which you can print on the display. Then divide the number by 10 and loop again. And then finally when the loop is done and what remains of the number is a digit smaller than 10, print that digit too.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to parse a file that has following data eg:
MAGICNUMBER 400
4 is = 0x34
0 is = 0x30
4
0
0
are different unsigned chars
what i want is those different chars to be converted into
unsigned int x = 400;
when parsing them into my program i want to merge them into one integer i tried bitshifting but it didn't work and i probably did it very wrong and got a very large number probably due misunderstanding of something, what i'm susposed to do to merge those numbers without string tricks and without using std but only using bitshift with a explanation how it works?
Each digit is c - '0'. When you get a new digit, you know that prior ones are one decimal place greater, so you multiply the current number by 10 and add the new digit:
char *s = "400";
int sum = 0;
while(*s >= '0' && *s <= '9') {
sum = 10 * sum + (*s - '0');
s++;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am currently working on a school project that is a circuit simulator. One of the components to this circuit can be a multiplexer, which has n inputs, log2(n) selectors, and 1 output.
The way I determine which output is needed is by doing the following:
Generate (# of selectors) bit gray code table, and loop through the table and compare to the values of the selectors. Whichever row is a match is the output needed.
However, for larger multiplexers (16:1, 32:1), this becomes quite slow. Is there a more efficient way to get the output needed without having to compare every single possible graycode possibility?
So you wish to construct a unique number in the range [0, 2n) (the index of the selected input) from n inputs (the selector signals) which are in the range [0, 1].
That's binary code!
Assign one of the selector signals the value 1, the next signal the value 2, then 4, and so on. Add them together. Select the corresponding numbered input.
unsigned selectedInput = 0;
if (selector1) selectedInput += 1; /* or |= */
if (selector2) selectedInput += 2;
if (selector3) selectedInput += 4;
And so on. In the generic case:
unsigned selectedInput = 0;
for (int i = 0; i < selectorCount; ++i)
if (selectors[i]) selectedInput |= 1u << i;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a small simple school project for c programming. I am supposed to write a number and get the reverse of it and I couldn't understand how this code works. So, I need someone to explain to me what happens in the while loop, and why we use these operations, and whats %10?
Thanks!
/* Reverse number using while loop */
#include<stdio.h>
int main()
{
int num, reverse=0;
scanf("%d",&num);
while(num > 0)
{
reverse= reverse*10;
reverse = reverse + (num%10);
num = num/10;
}
printf("%d",reverse);
return 0;
}
'While loop' repeats the number of entered numbers.
'num%10' means the remainder of num divided by 10. This is the process to know the numbers at the end.
For example, if you enter '123' then while loop will repeat 3 times.
The first step, reverse -> 3, num -> 12
Second, reverse -> 30 -> 32, num -> 1
Third, reverse -> 320 -> 321, num -> 0
Therefore you can get the reverse number!
In your code while loop is the place where the digits of the entered number get reversed and stored as another number - reverse. In order to do that the last digits of the original number need to be accessed first. The % modulo operator returns the remainder of the division. Since you are dividing the original number by 10 you get the last digit of it, which is the remainder. We need to store that digit as the first of the reversed number and albo somehow get to the next digits of the original number. To store consequtive digits you multiply currently existing reversed number by 10 and add the next digit. That way you add next digit to the right of reverse, eventually, after adding all digits, getting the complete reversed number. Getting to the next digits of the original number uses the property of the division / operator which leaves the quotient of the division. When dividing by 10 it "cuts off" the last digit of the given number. That also explains the condition in the while loop: when your number becomes 0 it means you have gone through all of its digits.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a relatively simple program that asks for user input between a range and then checks it using Do - while loop.
int n;
do
{
print ("hello, world\n");
n = getvalue() //just making this syntax up.It takes value from user
}
while (n<0 && n>99);
print ("%d\n",n);
I want to prompt the user to input another value if he/she enters either a negative number or a triple digit number (or higher). But the condition within while is not being validated.
What the output looks like:
No matter what number I enter, it get printed. i.e. -2 is printed as -2, 101 as 101 and 50 as 50. Ideally, -2 should prompt user to enter a number again, so should 101 and only 50 should print out.
I think the problem is your rather cryptic condition of n < 0 && n > 99, which can't reasonably be satisfied.
You need to look more closely at the logic of your loop. If you want to prompt the user again if they enter a number less than zero or greater than 99, you need to use the logical or operator ||.
while (n < 0 || n > 99);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a char array composed of elements 5 3. The array is used to represent number 53. What should be the approach to convert this number of three chars to its binary equivalent? I am implementing this in C, later on it will need to be rewritten in assembly. The solution I seek should be purely low stuff work without any helper libraries.
I am basically stuck with an idea to convert separately 5 and 4 (via mapping 5 and 4 to their ascii equivalents). Yet the idea would not work for sure. I have another idea to convert char '5' to int 5 by right shifting the byte by 4. Same with 4. Then multiply 5 by 10 and add 4, and then use division by two algorithm to find remainder and compose the binary number.
In C:
int asciToInteger(char *c)
{
int result = 0;
while (*c)
{
result *= 10;
result += (*c - '0');
c++;
}
return result;
}
Assumes input is valid.
You can get a head start on the assembly language version by compiling with certain switches which will output as ... assembly language! For example in GNU C: gcc -S -c ascii2int.c.