Results 1 to 39 of 39

Thread: operator

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    operator

    how could i translate this

    Code:
        For temp = 1 To 8
            send(temp) = 2 ^ (temp - 1)
        Next temp
    into C++...what is the ^ in C++, for the powers?
    Thanks
    Amon Ra
    The Power of Learning.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <cmath>
    
    void somecode() {
        for(temp = 0; temp < 8; temp++) {
            send[temp] = std::pow(2, temp);
        }
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    thanks parksie..just figured it out...how woud i write my own pow function though?
    thanks
    Amon Ra
    The Power of Learning.

  4. #4

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    nevermind...i found a way...thanks again
    Amon Ra
    The Power of Learning.

  5. #5

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    actually, could you give me an example, cuz you might have a better one
    Amon Ra
    The Power of Learning.

  6. #6

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i am trying to use a template
    Amon Ra
    The Power of Learning.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    integer powers?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    yup
    Amon Ra
    The Power of Learning.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    i know an algoritm that will do it with log2(power) multiplications and bitshifts
    was trying to write some pseudo for it but ended up writing the whole thing
    PHP Code:
    y=x;
    for (;
    power; ){
     if (
    power 1)
      
    y*=x*y;
     else
      
    y*=y;
      
    power >>1;

    haven't tested it yet though, but should work
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i am not sure what bit shiftings are for? i know they do something with the value, but i am not sure what...
    in
    Code:
    int a=8;
    a >> 2;
    whats the point of doing this...what willl a end up being?
    thanks a lot
    Amon Ra
    The Power of Learning.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    int a = 8; // 1000 in binary
    
    a = a >> 2; // Shift 2 places right = 0010 in binary, 2 in decimal :)
    It's basically a quick way of doing power-of-2 multiplication/division.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    isn't there other ones like & and | ?
    Thanks again
    Amon Ra
    The Power of Learning.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, those are the masking operators (don't know what they should REALLY be called):
    Code:
    int x = 9; 1001
    int y = 11; 1011
    
    x & y = 1001;
    x | y = 1011;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    bit shifting shifts bits, so as a number is stored binary, if you shift n bits right the result will be the same as integer division with 2^n

    the code doesn't work, when i explore it to show an example for you, the bit reading needs to be reversed, so i'll be right back
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    thanks a lot guys...it is a bit clearer now..
    Amon Ra
    The Power of Learning.

  16. #16

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    so if i do

    Code:
    int i=1011;
    int j=0001;
    int n;
    
    n = i & j;
    n would be equal to 0001, right?
    Amon Ra
    The Power of Learning.

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    parksie, isn't there an ASM instruction to get the integer log2 of a value, that is the highest set bit?
    or can you reverse the bits somehow?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by Amon Ra
    so if i do

    Code:
    int i=1011;
    int j=0001;
    int n;
    
    n = i & j;
    n would be equal to 0001, right?
    yep, thats bitwise and
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    ok cool..
    ok look, this is what i have so far, using what you gave me...i must have messed up somewhere

    Code:
    template<class PA> inline
    PA ipow(PA x, int y)	{
    	int i = x;
    
    	for (int z=0 ; z < (y +1); )	{
    		if (y  &1)
    			i *= x*i;
    		else
    			i *= i;
    	}
    		return (i);
    }
    apparently when i run, it enters an infinite loop
    Amon Ra
    The Power of Learning.

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well you modified it pretty much, anyways, the algoritm is the other way around, that is left to right not right to left bit reading
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    yea i modified it to see if it would stop the loop, but doesn't...what's wrong?
    Amon Ra
    The Power of Learning.

  22. #22

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    cuz when u put
    Code:
    for (; y;) {.....
    isn't it normal that you have an endless loop?
    Amon Ra
    The Power of Learning.

  23. #23
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    here this should work

    PHP Code:
    if (power){
     
    mask=256;//or higher max power
     
    for(;!(mask&power);mask>>1); // or something that finds the highest  set bit
     
    y=x;
     
    mask>>1;
     for (;
    mask; ){
      if (
    mask&power)
       
    y*=x*y;
      else
       
    y*=y;
      
    mask>>1;
     }
    else
     
    y=1;

    say you want to take the 10'th power of x
    10 is 1010 binary
    4'th bit is 1: y=x
    3'rd bit is 0: y=x*x
    2'nd bit is 1: y=(x*x)*(x*x)*x
    1'th bit is 0: y=((x*x)*(x*x)*x)*((x*x)*(x*x)*x)
    analoguous with
    y=x^10=x^5^2=(x^2*2)^2
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by Amon Ra
    cuz when u put
    Code:
    for (; y;) {.....
    isn't it normal that you have an endless loop?
    not if y has a chance to get 0, in my case mask is bitshifted until all bits are 0
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    ok thanks...i 'll try to put that in a function...thank you very much!
    i appreciate...=)
    Amon Ra
    The Power of Learning.

  26. #26

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    what should i return in my function?
    Amon Ra
    The Power of Learning.

  27. #27

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    there, i got it...
    thank you so much!
    Amon Ra
    The Power of Learning.

  28. #28

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    here is the final function i am using..
    Code:
    template<class PA> inline
    PA ipow(PA x, int y)	{
    	int mask=256, ret;
    	if (y)	{
    		for (; !(mask & y); mask >>=1);
    		ret = x;
    		mask = mask >> 1;
    
    		for(;mask;)	{
    			if (mask & y)
    				ret *= x*ret;
    			else
    				ret *= ret;
    			mask = mask >>1;
    		}
    	}
    	else
    		ret = 1;
    		return ret;
    }
    thanks a lot
    Amon Ra
    The Power of Learning.

  29. #29

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i think i read the function too many times, so i got confused

    Code:
    template<class PA> inline
    PA ipow(PA x, int y)	{
    	int mask=256, ret;
    	if (y)	{
    		for (; !(mask & y); mask >>=1);
    		ret = x;
    		mask = mask >> 1;
    
    		for(;mask	{
    			if (mask & y)
    				ret *= x*ret;
    			else
    				ret *= ret;
    			mask = mask >>1;
    		}
    	}
    	else
    		ret = 1;
    		return ret;
    }
    i tried figurin out what each line does...do you mind telling me? the bitshifting part confuse me..
    Thanks again
    Amon Ra
    The Power of Learning.

  30. #30

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    great!! i got it! thank you very much guys
    i apreciate
    Amon Ra
    The Power of Learning.

  31. #31
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    To save all disasterous mischiefs and to rid of endless looops, please use a while loop rather than a for loop.

    Code:
    template<class PA> inline
    PA ipow(PA x, int y)	{
    	int mask=256, ret;
    	if (y)	{
    		for (; !(mask & y); mask >>=1)
    			continue; // dont forget this...
    		// or
    		// while( !(mask&y) ) mask >>= 1;
    		ret = x;
    		mask = mask >> 1; // or mask >>= 1;
    
    		while(mask) {
    			if (mask & y)
    				ret *= x*ret;
    			else
    				ret *= ret;
    			mask = mask >>1; // or mask >>= 1;
    		}
    	}
    	else
    		ret = 1;
    		return ret;
    }
    that should be more stable

    What each line does:

    first:
    int mask=256;
    int ret;

    that declares 2 variables. mask that is 256 (0001 0000 0000, 0x100)

    then you have:
    if(y)
    that means if (y != 0 && y != false && y != NULL ) basically it means that y exists and y doesnt equal 0.

    the first for loop which is kind of fruity looking just does the following:

    if mask and y "not equal to 0" then (basically keep dividing by 2 as long as mask and y are not the same)

    ret = x; // duh!! set ret to equal x

    mask = mask >> 1; // same as mask = mask/2;

    then u have another fruity for loop, i prefer a while loop since it looks much clearer and its better suited for the loop itslef.

    while(mask) "loop while mask <> 0"

    if mask and y then
    ret = ret * x * ret; // note: x *= y is the same as x=x*y;
    else
    ret = ret * ret;
    end if

    mask = mask / 2;

    last but not least, if y was originally 0, just return 1.

    thats about it. I hope you found this usefull.

    BTW: the function is a template which means that it can be used for int, float, double, long, short, etc... and also, the word inline tells the compiler to compile this function everywhere that it sees the call of the function ipow(somevalue, thepower);. this is done to make the pow function as fast as possible.

    -MoMad
    Last edited by MoMad; Sep 16th, 2001 at 07:14 PM.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  32. #32
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Furthermore, Take a look at:

    Arithmatic Operators in C++



    The function itself can be simplified much more but the biggest reason ppl use C++ is that extra push of speed and efficiency so doing: mask = mask / 2; is not as fast as mask >>= 1; bitwise operators are extremly fast because all they do is take a look at the binary number and do a fast "switching" of bits (on/off).
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  33. #33
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by MoMad
    [B]To save all disasterous mischiefs and to rid of endless looops, please use a while loop rather than a for loop.
    Doesn't matter if you use a while of a for, the middle statement will be identical in the while statement. It's a qwestions of taste actually, because anything you can do with while, you can do with for, and vice versa.

    that should be more stable
    no, not really

    then you have:
    if(y)
    that means if (y != 0 && y != false && y != NULL ) basically it means that y exists and y doesnt equal 0.
    Why would anyone need to know that? The reason I tested the base, was that X^0 is 1 for all X!=0. In other cases the solution had to be retrieved iteratively with multiplications.



    the first for loop which is kind of fruity looking just does the following:

    if mask and y "not equal to 0" then (basically keep dividing by 2 as long as mask and y are not the same)

    ret = x; // duh!! set ret to equal x
    Why the heck would i do that? guess? No? I can see that you can't see the forest for the trees
    Actual Reasons: I want to find first set bit and for the second at least one factor x is used, the set bit indicating 1 gives y*=y*x giving x. For each odd bit the additional factor x is included in the recursion to compensate the amount of factors.

    mask = mask >> 1; // same as mask = mask/2;
    Why? Because I need to check next bit, for next recursion.

    Does anyone know what i'm trying to achieve here?

    The algoritms obvious goal is to minimize the amount of multiplications on a power. A regular recursive multiplication would take n-1 multiplications for power of n, this algoritm reduces it to log2(n).

    Say we have X^68
    it could be written as
    X^34^2
    and further
    X^17^2^2
    (X^16*X)^2^2
    (X^8^2*X)^2^2
    (X^4^2^2*X)^2^2
    (X^2^2^2^2*X)^2^2

    notice the extra *X in there, that got there to compensate the odd value 17. If you take a look at the binary equivalent of 68:
    1000100
    you notice it's the 5'th from left, 3'rd from right, and when you look at:
    (X^2^2^2^2*X)^2^2
    the extra *X is at 5'th from left and 3'rd from right. Since the recursions goes from left to right, as you obviously need to start with the first X, the algoritms will start from the most significant bit as well, as it will be most significant in the recursion as a factor.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  34. #34
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Ahh, I see what u mean. But why do u keep talking about recursion, do you mean the loop? I dont see any recursions here, just nested loop.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  35. #35
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yes, essentially a inlined data recursion, most recursions can and should be written inline to save performance
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  36. #36
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    I still dont get it man. Its a LOOP the function doesnt call itself. So im lost.

    This is what i call recursion:
    Code:
    int recurse(int n) { if( --n > 0 ) recurse(n); return n; }
    Not this:
    Code:
    int norecurse(int n) { while( --n > 0 ) continue; return n; }
    Which, in both cases should return 0. What do u think kadaman?
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  37. #37
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Smile Yeah

    It is two implementations of the same algoritm, actually I have to agree with you, the first implementation is recursive while in the second the recursion is inlined to to a iterative solution.
    However I still think you can still talk about recursive algoritms, and the implementations of them as iterative
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  38. #38
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Ooohhh I see, so in iterating, you hope to save stack flow.

    Hmm, you're right about every recursive problem being able to be reduced to an iteration... but the whole point of using recursion is to show off how complicated you can make the simplest problem right??> lol... j/k. Yeah, I totally have to agree with you there.

    So again, you said something to throw me off... "the recursion is inlined"????? Whats that supposed to mean, that the recursion is in a form of a loop? Oh well, i guess we'll be on the same page for now
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  39. #39
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hehe, actually I don't know what you should call it then, doesn't matter anyway
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width