Quantcast
Channel: D3Scene
Viewing all articles
Browse latest Browse all 21080

Coding event - Beat the admin!

$
0
0
Hey members, especially coders of D3Scene!

Hereby I want to announce a special event that'll take place on D3Scene for the next 10 days [Until 19th of May, 2013].

What is this all about?

You will have to edit a .c file (Yes, it's about coding in C but any coder will be able to solve the coding part) which has a few tasks to solve, rated from 1 to 4 (1 being the easiest, 4 the most difficult task).
The tasks will be based on bit-operations to you'll be required to know about HEX, Binary and Decimal systems.
Now the problem is not really to solve the tasks, but to manage it with as little operations as possible [I will provide an example at the end to show you what an operation means].

So you might now ask how to proceed and why to do this. Well, easy answer:

You can try to be better than me and my solution. I'll show you my code in the end plus how many operations it took me!
If enough people participate I'll throw some prizes in! :)


Alright, enough talking. Here's the code you'll have to edit:
 
Code:


/*
 * TO-DO: Modify the following functions according the coding rules.
 * TO-DO: Modify the following functions according the coding rules.
 * TO-DO: Modify the following functions according the coding rules.
 * TO-DO: Modify the following functions according the coding rules.
 */


/*
 * bitNor - ~(x|y) using only ~ and &
 *  Example: bitNor(0x6, 0x5) = 0xFFFFFFF8
 *  Legal ops: ~ &
 *  Max ops: 8
 *  Rating: 1
 */
int bitNor(int x, int y) {
  return 2;
}


/*
 * negate - return -x
 *  Example: negate(1) = -1.
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 5
 *  Rating: 2
 */
int negate(int x) {
  return 2;
}


/*
 * isEqual - return 1 if x == y, and 0 otherwise
 *  Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 5
 *  Rating: 2
 */
int isEqual(int x, int y) {
  return 2;
}


/*
 * isNonNegative - return 1 if x >= 0, return 0 otherwise
 *  Example: isNonNegative(-1) = 0.  isNonNegative(0) = 1.
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 6
 *  Rating: 3
 */
int isNonNegative(int x) {
  return 2;
}


/*
 * reverseBytes - reverse the bytes of x
 *  Example: reverseBytes(0x01020304) = 0x04030201
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 25
 *  Rating: 3
 */
int reverseBytes(int x) {
  return 2;
}


/*
 * multFiveEights - multiplies by 5/8 rounding toward 0.
 *  Examples: multFiveEights(77) = 48
 *            multFiveEights(-22) = -13
 *  You can assume |x| < (1 << 29)
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 12
 *  Rating: 3
 */
int multFiveEights(int x) {
  return 2;
}


/*
 * abs - absolute value of x (except returns TMin for TMin)
 *  Example: abs(-1) = 1.
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 10
 *  Rating: 4
 */
int abs(int x) {
  return 2;
}


/*
 * logicalNeg - implement the ! operator, using all of
 *              the legal operators except !
 *  Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
 *  Legal ops: ~ & ^ | + << >>
 *  Max ops: 12
 *  Rating: 4
 */
int logicalNeg(int x) {
  return 2;
}



Here is an example on how it has to look like:
 
Code:



/*
 * minusOne - return a value of -1
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 2
 *  Rating: 1
 */
int minusOne(void) {
  return 2;
}


This is how to solve it:
 
Code:

int minusOne(void) {
  return ~0;
}


Notice that I only used operations that were legit. If there is no '-' listed you're not allowed to use it, obviously (Would come in handy in some tasks though..).
It took me exactly ONE (1) operation (~).
Using '=' or declaring variables doesn't count as operation.
Another example:
 
Code:

int secondExample(void) {
  int var = 4;
  int result;
  result = (var >> 2);
  return result;
}


The return will be 2 and there was one operation (>>) used!

I hope that helped you on your way and now: Go ahead and solve the tasks! :)


Rules:
- When you're done PM me your code
- It's not allowed to call any other functions within or from outside of each function.
- Only use operations that are stated to be allowed!
- Have fun! :)

Here's a list of how many operations it took me to solve the tasks:

bitNor: 3
negate: 2
isEqual: 2
isNonNegative: 2
reverseBytes: 12
multFiveEights: 6
abs: 4
logicalNeg: 6

Come at me brooo! ;)
In case this is too easy for you I have some more tasks that are very difficult to solve.

Viewing all articles
Browse latest Browse all 21080

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>