Author Topic: I need help: Bits, bytes, and exponents  (Read 5076 times)

0 Members and 1 Guest are viewing this topic.

Offline macweirdo

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-3
    • View Profile
I need help: Bits, bytes, and exponents
« 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

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: I need help: Bits, bytes, and exponents
« Reply #1 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...
« Last Edit: November 27, 2011, 02:13:44 pm by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: I need help: Bits, bytes, and exponents
« Reply #2 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
}
« Last Edit: November 27, 2011, 02:18:49 pm by Netham45 »
Omnimaga Admin

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: I need help: Bits, bytes, and exponents
« Reply #3 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;
}
« Last Edit: November 27, 2011, 07:01:34 pm by jacobly »

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: I need help: Bits, bytes, and exponents
« Reply #4 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 ;)
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: I need help: Bits, bytes, and exponents
« Reply #5 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
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline macweirdo

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-3
    • View Profile
Re: I need help: Bits, bytes, and exponents
« Reply #6 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
« Last Edit: December 11, 2011, 09:03:45 pm by macweirdo »