Thread: Question pertaining to limits...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Hampshire, England
    Posts
    8

    Question pertaining to limits...

    This is kind of an odd question, but me and a friend are learning C++ together to program a game. Today he said to me he was slightly worried that a lot of things have a limit of 255 such as number of objects/images. I dont quite believe him and thought I would ask here...

    Is there a limit to images, sounds or anything and is that limit 255?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Nope.

    Though eventually you'll run out of memory or processing power, so it's generally a good idea to limit the numbers of objects within your program to what it can comfortably manage.

    Programmers often choose their limits on powers of two for some reason
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    255 (or 256) is a pretty popular number with anything regarding 8-bit(one byte) stuff.

    If you don't understand why that is, you should learn how to read binary code to better understand how bits and bytes work.

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    A little snippet from my 70-page tutorial about programming games: a lovely primer on binary digits. I lost a lot of the formatting and such, so it's not as pretty.

    Computers only translate the presence of electricity in their circuits. The presence of an electric current in a circuit is represented by a 1, and the absence of electricity is represented with a 0. Since computers can only translate two different states, they use base 2 counting system to count and represent numbers and information.

    We people use base 10 to count (probably because we have 10 fingers). We have 10 digits we use to count: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. In base two, there is only 0 and 1. Here’s how counting in our base 10 system works. We have a 1’s place, a 10’s place, and so on, going up and up in powers of 10s.

    10^0 = 1 //One's place
    10^1 = 10 //Ten's place
    10^2 = 100 //Hundred's place


    So if we have the number 5402, we have 5 thousands, 4 hundreds, 0 tens, and 2 ones. Now let’s try this with base 2. It‘s like base 10, only it works in powers of 2. Naturally, it will take a lot more digits to represent values than base 10 representations would because the powers of 2 are much smaller than the powers of 10.

    Here’s the table of how binary digits work:

    2^0 = 1
    2^1 = 2
    2^2 = 4
    2^3 = 8
    2^4 = 16
    2^5 = 32
    2^6 = 64
    2^7 = 128
    //All these numbers added together is 255.

    Remember that any number to the 0th power is 1! This is important to remember. The first place at the right will ALWAYS be the 1’s place, and you’ll work to the left in powers of the base you’re using from there.

    This might clarify why any number to the 0th power is 1.

    2^3 = 8
    2^2 = 4
    2^1 = 2
    2^0 = 1
    2^-1 = 1/2
    2^-2 = 1/4
    2^-3 = 1/8


    As you work from the right side of the table to the left, you see that the values of the powers are always being divided by two. 8/2 = 4. 4/2 = 2. And so, 2/2 = 1. 1 divided by two is 1/2, and divide that by two again, and you get 1/4.

    Let’s try converting the number 170 to binary code. To do this, we start from the left side of the table, to the right. We find the greatest number that will go into 170 ONCE and only ONCE. 128 can go into 170 once, so we place a 1 for our first digit. We then take the remainder, which is 42, and move it to the next column. Does 64 go into 42? No. So we place a 0 in that column. Let’s move on and try 32. 32 can go into 42, so we can place a 1 there, which leaves us only 10. Moving on, 16 cannot go into 10 so a 0 is placed there. 8 goes into 10, and we place a 1 there, leaving us with only 2 for the twos column.

    Our resulting number is 1010 1010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM