Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Small Programming Challanges :)
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Fri Jun 25, 2010 8:52 am    Post subject: Re: Small Programming Challanges :) Reply with quote

Montycarlo wrote:
I'm not a java coder so I don't know how the inverse-bit operator works in syntax.

Are either right?

I think '~' will invert all bits and '!' will take the negative (which is inverting all bits and add one).
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jun 25, 2010 11:53 am    Post subject: Reply with quote

Factorial. Mine is the fastest.

Code:
#include <iostream>

template <unsigned int N>
struct Factorial {
   enum { Result = N * Factorial<N-1>::Result };
};

template <>
struct Factorial<0> {
   enum { Result = 1 };
};

int main()
{
   std::cout << Factorial<30>::Result << std::endl;
   return 0;
}
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Fri Jun 25, 2010 2:48 pm    Post subject: Reply with quote

Nice solution, I wanted to give this challange later (factorial at compile time) but I guess I'll just think of another challange with classes and/or templates.

Anyway, Flyte: I did not request you to write a factorial function/template structure, look at the whole challange next time and not just the title. Rolling Eyes

In fact, I'll give you guys a hint and say calculating the factorial in this challange isn't even needed.

Montycarlo wrote:
Deltron Z wrote:

7. (taken from a java book)
Without running, what will be the output of:

Code:
public class JoyOfHex {
public static void main(String[] args) {
System.out.println(Long.toHexString(0x100000000L + 0xcafebabe));
}
}

Explain. (3 points)

It will define a public class with a single member; the static function main which will print out 1cafebabe to the handler at System.out.
The function can be accessed and called using the dot notation:
Code:
//Reference: JoyOfHex.main
//Call: JoyOfHex.main()


No, look at my solution on top of the 5th page.

Montycarlo wrote:

Code:
public uint abs(int input){
    //Assuming value is stored in 'Two's compliment' format
    return input & (!input)
}   

I'm not a java coder so I don't know how the inverse-bit operator works in syntax.

Are either right?

No. remember that x & NOT x = 0. (1 & 0 = 0, 0 & 1 = 0)
Use the sign bit, this is the way to do this.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jun 25, 2010 4:11 pm    Post subject: Reply with quote

Deltron Z wrote:
Anyway, Flyte: I did not request you to write a factorial function/template structure, look at the whole challange next time and not just the title. Rolling Eyes


Code:
11. Factorial. (2~5 points depending on solution)
Factorial (n!) means n × (n − 1) × ... × 3 × 2 × 1.
n! can also be described as the multiplication of prime numbers powers, for example:
5! can be described as 3 1 1 because 5! = 2^3 * 3^1 * 5^1.


I do not see a question there. In fact, I do not even see you requesting anything. Perhaps you should finish writing the question before you make a fool of yourself.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Jun 25, 2010 7:06 pm    Post subject: Reply with quote

Code:
cdq             
xor eax, edx
sub eax, edx


so
Code:
inline int abs(int x)
{
   __asm
   {
      mov eax, dword ptr [x]

      cdq
      xor eax, edx
      sub eax, edx
   }
}


even the branching code will compile to branchless, i kinda cheated since i looked at the disassembly first.



otherwise, you can find this all over the place, it's well known
Code:
inline int abs(int x)
{
   int mask = x >> 31;
   return (x ^ mask) - mask;
}


but it hardly matters, the compiler does this optimization for you.
Back to top
View user's profile Send private message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Sat Jun 26, 2010 7:09 pm    Post subject: Reply with quote

slovach wrote:
Code:
cdq             
xor eax, edx
sub eax, edx


so
Code:
inline int abs(int x)
{
   __asm
   {
      mov eax, dword ptr [x]

      cdq
      xor eax, edx
      sub eax, edx
   }
}


even the branching code will compile to branchless, i kinda cheated since i looked at the disassembly first.



otherwise, you can find this all over the place, it's well known
Code:
inline int abs(int x)
{
   int mask = x >> 31;
   return (x ^ mask) - mask;
}


but it hardly matters, the compiler does this optimization for you.

Aren't you violating the "No conditions" requirement by using XOR?

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sat Jun 26, 2010 7:25 pm    Post subject: Reply with quote

Odecey wrote:
Aren't you violating the "No conditions" requirement by using XOR?


Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements.
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Sat Jun 26, 2010 10:51 pm    Post subject: Reply with quote

Flyte wrote:
I do not see a question there. In fact, I do not even see you requesting anything. Perhaps you should finish writing the question before you make a fool of yourself.

My bad.

Write a program whose input is N and the output is the representation of N! as primes powers.
Back to top
View user's profile Send private message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Sun Jun 27, 2010 7:41 am    Post subject: Reply with quote

Flyte wrote:
Odecey wrote:
Aren't you violating the "No conditions" requirement by using XOR?


Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements.

Then I guess my next question is, how far is too far, and how is that made clear? Personally, I think Deltron Z should have rephrased it to something more like "Absolute value- Bitwise operators: Write a function which returns the absolute value of a number by only using bitwise and arithmetic operators."

Deltron Z: Why haven't you awarded Slovach points for the solution?

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Sun Jun 27, 2010 11:30 am    Post subject: Reply with quote

Odecey wrote:
Flyte wrote:
Odecey wrote:
Aren't you violating the "No conditions" requirement by using XOR?


Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements.

Then I guess my next question is, how far is too far, and how is that made clear? Personally, I think Deltron Z should have rephrased it to something more like "Absolute value- Bitwise operators: Write a function which returns the absolute value of a number by only using bitwise and arithmetic operators."

Deltron Z: Why haven't you awarded Slovach points for the solution?

I don't really notice points anymore, I begin to think it's a bad idea and just write the solver's name next to the challange.

Let's define a condition an operation that would change and use the flags. C: if, for, while, etc... ASM: CMP, TEST, Conditional Jumps, etc...
This also includes functions that has conditions in them (power, square root, etc...)
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Jun 28, 2010 2:55 pm    Post subject: Reply with quote

how does power and square root have conditions in them ? it probably just gets translated to a 'fmul st0, st0' or something
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Mon Jun 28, 2010 10:31 pm    Post subject: Reply with quote

That's only a square. Rolling Eyes Rolling Eyes
A power requires loops, and if you look at the function's disassembly you will see conditions.

Anyway, stop trying to find loopholes and try to solve. Razz
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Jun 28, 2010 11:03 pm    Post subject: Reply with quote

Deltron Z wrote:
Let's define a condition an operation that would change and use the flags. C: if, for, while, etc... ASM: CMP, TEST, Conditional Jumps, etc...
This also includes functions that has conditions in them (power, square root, etc...)


You're aware that quite a few assembly instructions modify and use flags that aren't conditional expressions, right? Arithmetic instructions and the carry flag, for example.

Slugsnack wrote:
how does power and square root have conditions in them ? it probably just gets translated to a 'fmul st0, st0' or something


Square root probably has a '>= 0' check.
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Tue Jun 29, 2010 4:48 am    Post subject: Reply with quote

Flyte wrote:
You're aware that quite a few assembly instructions modify and use flags that aren't conditional expressions, right? Arithmetic instructions and the carry flag, for example.

Just solve the damn challanges. Rolling Eyes
Back to top
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Tue Jun 29, 2010 11:34 am    Post subject: Reply with quote

max(a,b):(a+b+abs(a-b))/2
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 6 of 7

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites