Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 23 24 [25] 26 27 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95915 times)

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #360 on: September 03, 2010, 04:44:30 am »

Minor necro, but I have a question on bitshifts. I hope you don't mind.

I'm starting to see a pattern in them. Bitshifting x by y bits is equal to x*2y, and a bitshift right is x/2y, right? Or am I completely off my rockers?

Sorry if I'm being ignorant here, as I'm rather new to this stuff.
Yep, and super efficient as well. Although your compiler/pow function might already translate a Pow with a power of two as a bitshift.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #361 on: September 03, 2010, 09:41:56 am »

Probably depends on the optimization level.
Logged
Eh?
Eh!

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #362 on: September 13, 2010, 02:02:09 pm »

The Pie is back, and now with even worse code than before, if keep this up, I could be the worst there is. :-P

Anyway, can someone explain why this only works for numbers from 0 to 1023?
Code: [Select]
int int2bin (int input) {
  int output = 0;
  int i = 0;

  while (input - pow(2,i+1) >= 0) i++;
  while (i >= 0) {
    if (input - pow(2,i) >= 0) {
      output = output * 10 + 1;
      input = input - pow(2,i);
      i--;
    } else {
      output = output * 10;
      i--;
    }
  }
  return output;
}
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #363 on: September 13, 2010, 02:14:40 pm »

I'm still confused by enumerated types. Can anyone explain them to me?
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #364 on: September 13, 2010, 02:26:22 pm »

I'm still confused by enumerated types. Can anyone explain them to me?
If I understand it correctly, it's just a list of stuff where each item in the list has number.
Say you have enum blah {one, two, three}, blah.one + 1 = blah.two, and blah.one = "one". Not so sure about the last part.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #365 on: September 13, 2010, 03:00:04 pm »

The Pie is back, and now with even worse code than before, if keep this up, I could be the worst there is. :-P

Anyway, can someone explain why this only works for numbers from 0 to 1023?

You reached the end of the universe.
Code: [Select]
  #include <limits>
  std::cout << std::numeric_limits<int>::max()    << std::endl;
And compare that number to the previous number (the output of 1023). Then think of what the next number is going to be.

That number is the max of an int. If you want more, use double, or a string.


Enums are simply names for integers, so you can type
if(checkError() == OVERFLOW_ERROR){ bla }
instead of
if(checkError() == 4){ bla }

They are interchangeable with ints (might need a cast to prevent warnings), but doing actual arithmetic with them is not what they're for. They're to make your code more readable.

Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #366 on: September 13, 2010, 03:06:47 pm »

Enumerated types vary from language to language. Neither explanation is correct; generally speaking, an enumerated type is a type that is enumerated. Note the definition of enumerate: http://dictionary.reference.com/browse/enumerate. Thus, an enumerated type is a type that lists all possible values of that type. C++ enums are fairly useless in their current state; C++0x will actually make them less meaningless. In Java or C#, an enumerated type can only take on a certain set of values defined within the class.
« Last Edit: September 13, 2010, 03:08:37 pm by Normandy »
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #367 on: September 13, 2010, 03:08:58 pm »

That number is the max of an int. If you want more, use double, or a string.
Aye, thanks. I wasn't paying attention to the output.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #368 on: September 13, 2010, 04:16:22 pm »

Oh. Does that mean I can skip that one?

I'm learning C++ by the way.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #369 on: September 14, 2010, 04:50:38 pm »

Oh. Does that mean I can skip that one?


hell no.

Actually I have no idea what you're doing.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #370 on: September 14, 2010, 05:47:47 pm »

Oh. Does that mean I can skip that one?


hell no.

Actually I have no idea what you're doing.
Learning enums I would assume. It is a good idea to do it, since various chunks of code you encounter may use them, and for that reason alone it is good to know how they work. As for use, they seem to be mostly for readability rather than adding functionality to your code, since
Code: [Select]
object_state = object_state_running;is much easier to understand than
Code: [Select]
object_state = 2;
Edit after the fact: Turns out I am ending up using them in my current code in order to keep track of the (unchanging) positions of certain important textures within a container. WindowTop is much easier to figure out the meaning of than its value of 6.
« Last Edit: September 14, 2010, 08:23:35 pm by alway »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #371 on: September 15, 2010, 03:27:25 am »

Also: multiple options. If you enum as powers of two, you can use multiple of them as one parameter like this:

initEngine(OPEN_DOOR | START_ENGINE | GO_TO_WORK);

As each encodes a bit and they're added together by the OR operator. The function can then use an AND to check which of the bits were set.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #372 on: September 15, 2010, 07:46:40 am »

God, it's gonna take forever for me to understand those bitwise operators. I understand how they work, more or less, but is there some kind of book/tutorial/guide or whatever you want to call it that explains how you use them?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #373 on: September 15, 2010, 07:57:31 am »

http://www.cprogramming.com/tutorial/bitwise_operators.html

Tadaa. It explains how they work, how you can use them, and examples of situations where they can be useful.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #374 on: September 15, 2010, 08:14:32 am »

That's pretty cool, thanks.
Logged
Pages: 1 ... 23 24 [25] 26 27 ... 78