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 9 years ago.
Improve this question
Is there a way to improve this line of code? I think this conversion slows down my program. I think something like bit shifting can be used, by I am not sure.
(uint16_t)(0.8*(float)(Value) ?
EDIT: I need to program the atmega8 microcontroller. My teacher said that this line of code would require more processing power and that there is a simpler way of doing this with bit shifting.
(I'm assuming that Value is an int as well.)
0.8 times x is the same as (4 times x) divided by five. Multiplying an integer by a power of 2 can be done very quickly with a bit-shift. You can do this explicitly if you know how, but any modern compiler will automatically optimize int x = ...; x *= 8; to a bit-shift for you, so you don't need to worry about it. Details about how to do this by hand are widely available if you are interested.
So one thing to try is (Value * 4) / 5.
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 2 years ago.
Improve this question
What would sizeof operator return for int data type in 16bit processor system ?
I'm thinking it would be 2 bytes since that's max int that can be represented in system
This answer is about C and not C++. They are two different languages. It may or may not be applicable to C++.
The only thing the standard says about the size is that it should be at least 16 bits. It has nothing to do with the hardware. A compiler may use 16-bit ints on a 32 bit system. The hardware does not dictate this. The compiler constructors typically make optimizations towards certain hardware for obvious reasons, but they are not required to.
An int should be able to hold all values in the range [-32767, 32767], although it's common with [-32768, 32767] on 16 bit systems that are using two complement representation, which almost all modern system does.
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 4 years ago.
Improve this question
I have a problem in C, I am not allowed to use floats as the microcontroller it will be flashed does not support that data type. Now all my integers are being rounded off as it should. How do I handle this case?
A short research indicates using bit wise operation such as left shift and right shift. I know what are these operations. But I do not know how to use these operations to achieve what I want.
Another possibility is the Q number format.
You will get some results if you google "Q number format" or some variations.
It is often used for some DSP related topics in C. Here another blog post that explains that number format and here is an example code implementation for q-numbers in C.
In general you can say that q-numbers represent a number between -1 and 1 without using floating point arithmetic.
Normally a microcontroller don't have a floating point unit, everything works with integers. But its up to you which unit you like for your integers.
For example:
100 could be 100 cm or 1,00 m
1000 could be 100,0 cm or 1,000 m and so on..
Please have a look at the description:
electronic.stackexchange
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 7 years ago.
Improve this question
I need to hold a value of range 10^20 in C. Heard that the big int in C can hold such big values. How to declare and use the big int in C.
Does anybody know of an easy way to do that? Any help would really be appreciated!
You can use type unsigned long long, the range is at least 0..18446744073709551615, but that's only 1.8E19, so slightly less than what you need. If you really want to go beyond 64 bits, you can check if your system supports 128 bit integers (as type __int128, __int128_t, int128_t or something similar) or you will need a multi-precision package such as GMP.
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 8 years ago.
Improve this question
I'm making a virtual machine in C and I was told that by converting the "assembly" code into hexadecimal or binary, I could speed up the execution. My question is, if I have a string and I encode it to numbers, how can it be faster than not encoding it when it adds and extra step to the execution in the VM?
--EDIT--
An example of the VM assembly is:
push 10 # Push the value 10 to the top of the stack
print # Print the value at the top of the stack
The encoded instructions look like this:
010a 0c
But the part I don't get is how encoding the assembly into instructions can be faster than not doing it because you have to decode them again. Please let me know if I'm wrong though.
Yes, encoding is better in the case on the re-use of the code.
Keep in mind that a function or piece of code is called many times, and so you loose little times for the first conversion and after you gain a lot of time when you start interpret the same code.
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 8 years ago.
Improve this question
A byte has 8-bits. Can it be larger in another system? Give an example.
Also, how many different types of calls such as writeint,writedec,writestring,writechar are there in assembly language.
Thanks.
Not any more. There was a time, yes, when there were systems without a fundamental 8-bit byte.
System where 1 byte != 8 bit?
how many different types of call such as writeint,writedec,writestring,writechar are there in assembly language
This question makes no sense. Assembly language is just a means of writing code that translates directly to machine instructions. call is just one of these instructions - it jumps to some other section of code, with the intent of returning to the place where the call was made.
The things you're referring to sound more like library routines - in which case there are any number of them, depending on the programming environment.