Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: macweirdo on November 27, 2011, 01:08:51 pm

Title: I need help: Bits, bytes, and exponents
Post by: macweirdo on November 27, 2011, 01:08:51 pm
Alright, so I want to read, for example, a single bit of data from a byte.

I tried
Code: [Select]
(L3+offset) and 2 to the power of bit_I_want where L3+offset is my byte of choice and "and" is logical and

but I can't figure out how to get # to the power of other# because ^ is now modulus.

So tl;dr how do you read a bit from a byte and how do you multiply a number by itself many times without using For(?

Thanks in advance :D
Title: Re: I need help: Bits, bytes, and exponents
Post by: Michael_Lee on November 27, 2011, 02:13:21 pm
To read a bit from a byte, use the Euler's e.

From the Axe documentation:
Quote
EXP1eEXP2

Gets the expression2-th bit of the 8-bit number in expression1. Unlike assembly, the leftmost bit (high order) is bit 0 and the rightmost bit (low order) is bit 7. The bit checking is modular. That's the Euler's constant "e".

So if we have
Code: [Select]
9->{L1+1}    .In bits, 0000 1001

For(A,0,7)
    Text(A*8,10,Ae{L1+1}>Dec)
End
...The screen would display something like
0 0 0 0 1 0 0 1

The '^2' (the small exponent 2 token) can multiply a number by itself once, and 'e^(' (e to the power of token) can multiply two to a given power, but I can't seem to find anything about actual exponents in the documentation (unless I'm being stupid).  Hmm...
Title: Re: I need help: Bits, bytes, and exponents
Post by: Netham45 on November 27, 2011, 02:18:37 pm
A function to get the power of a number would be pretty easy to make, if there isn't one handy.
Here's a C++ function; I don't know Axe:
Code: [Select]
double Pow(double number,double power)
{
for (power;power>0;power--)
   number*=number;
return number
}
Title: Re: I need help: Bits, bytes, and exponents
Post by: jacobly on November 27, 2011, 07:01:18 pm
A function to get the power of a number would be pretty easy to make, if there isn't one handy.
Here's a C++ function; I don't know Axe:
Code: [Select]
double Pow(double number,double power)
{
for (power;power>0;power--)
   number*=number;
return number
}

O(log n) instead of O(n) :D
Code: [Select]
double Pow(double number,double power)
{
result = 1
for (power;power>0;power>>=1) {
   if (power % 2)
      result *= number;
   number*=number;
}
return result;
}
Title: Re: I need help: Bits, bytes, and exponents
Post by: harold on November 27, 2011, 08:01:40 pm
Or exp(ln(base) * power), which actually handles non-integral powers instead of just pretending to do so but then blowing its cover by bitshifting a double ;)
Title: Re: I need help: Bits, bytes, and exponents
Post by: calc84maniac on November 27, 2011, 10:53:46 pm
Yeah, I'm pretty sure he meant the second argument to be an unsigned int :P

Anyway, Axe equivalent:
Code: [Select]
Lbl POW
1→r3
r2
While 1
If ^2
r3*r1→r3
End
r1²→r1
End!If r2/2→r2
Return r3
Title: Re: I need help: Bits, bytes, and exponents
Post by: macweirdo on December 11, 2011, 08:59:44 pm
Thanks guys!

Also, if you're really looking to get the exponent of a number, and you're really just doing 2^$something,
just bitshift 1 $something times.

I just don't like looping :(

EDIT: or wait, no
0010 is 2
0100 is 4

nvm, don't even need loops for that