The following code produces 2 different outputs based on the data types of j. I'm trying to understand the reason behind the difference. Can someone help?
If you printed the value output by pow to more than 2 decimal places, the answer would be more obvious.
The output from the pow function is being forced into an int, so everything after the decimal point is truncated. It looks like pow(2,2) is returning a value like 3.999999999997, instead of 4.000000000000.
The 3.999999999997 is printing as though it was 4.00, because the value is rounded.
When it is stored in the integer, it is truncated.
Notice that the inputs to pow are both floats, not ints.
Parameters
base: the number (float)
exponent: the power to which the base is raised (float)
It looks like pow(2,2) is returning a value like 3.999999999997, instead of 4.000000000000.
Isn't it supposed to be 4 ? any other operations for squaring?
Toggling bits? XOR is the way well i thought of it but not clear of implementation. I would like toggle/ignore every bit from the LSB to MSB in that order. any examples?
Not sure what you mean by "toggle/ignore" and I'm unclear why you're bringing floats into the discussion. Bit manipulation on floating point typed is fraught with all sorts of pitfalls.
Not sure what you mean by "toggle/ignore" and I'm unclear why you're bringing floats into the discussion.
To tell you a bit more.. I'm building a digital school attendance register with an arduino. Now there are 6 - 8 classes every day and I want to use 1 variable of type byte for each student to store his state(present/absent). Now assuming there are 8 classes each bit of a byte type would correspond to 1 class and this is exactly what I'm trying to toggle from '0' to '1' incase of a student being present and ignore it if the student is absent(so it stays as '0'). As for bringing floats, Its just that I was using the POW function and found it weird that it gave such results..thought someone here will know more about it.
Multiplication does it for me.
Well i have written my own function to take care of this but then wondering about the way POW function works...
Your weird results are caused by the conversion of floats to ints using truncation rather than rounding. Add some 0.5 at the places conversion is done and you should get more reasonable results.
That said, you really don't want to use pow() to isolate bits. Use the bit-shfit operators (<< and >>) instead.
#define setbit(where, whichbit) where |= 1<<(whichbit) #define clrbit(where, whichbit) where &= ~(1<<(whichbit)) #define togbit(where, whichbit) where ^= 1<<(whichbit)
These are standard programming constructs that you can find explanations for ... in lots of places. There are similar macros as part of the standard arduino environment. See bitRead, bitSet, bitClear, bitWrite
There are similar macros as part of the standard arduino environment. See bitRead, bitSet, bitClear, bitWrite
This is what i was looking for...Thanks!
That said, you really don't want to use pow() to isolate bits. Use the bit-shfit operators (<< and >>) instead.
Well i was working on a longer solution as i couldn't find the bitSet Functions. I was trying to raise 2 to the power of a increasing counter and then perform normal addition. This will control each of the bits as well only its a crude way of doing things.