Arrays and bits

This is a discussion on Arrays and bits within the C++ Programming forums, part of the General Programming Boards category; Ok, im reading C++ for Dummies and they say its plain english, but for someone who only knows HTML, it ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Exclamation Arrays and bits

    Ok, im reading C++ for Dummies and they say its plain english, but for someone who only knows HTML, it isnt. Now they talk about bits, like 0 is false and 1 is true. Can someone explain that to me?

    Second, can someone explain to me the creatings your own functions?

    Third, what does null mean?( '/o' )

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    When you use a computer, all that the computer can do is differentiate between 0's and 1's. It then translates that into usable information.

    A function is like a mini-program. It is run by your program. What type of value the function gives back when it is done is specified before the name. Then, inside the parenthesis, the information that the function needs to know to run is called the parameters. Sometimes the function may need no info to run. Then, at the end of the function after the code, if the function returns something (not void), it will say return and then some value. Here is a basic function that multiplies to numbers and returns that value.

    Code:
    #include <iostream>
    using namespace std;
    
    int multiply(int a, int b);/*names don't have to be the same as the names inputted into the function*/
    
    int main()
    { int num1, num2, product;
       cout<<"Input the first integer to be multiplied";
       cin>>num1;
       cout<<"Input the second integer to be multiplied"
       cin>>num2;
       product=multiply(num1, num2);
       cout<<"The product of "<<num1<<" and "<<num2
              <<" is "<<product;
       return 0;//main is a function too + returns an int
    }
    
    int multiply(int a, int b)
    {return a*b;}

  3. #3
    Unregistered
    Guest
    Think of it like this, the computer only knows two things, on and off. Like a light switch on the wall, on the lights come on, off the light go out. So false is off (0), and on is (1). For example:

    if (hours < 9)
    cout << "snap"; //if expression is true (1), it will do this one

    else
    cout << "pop"; //if expression is false (0), it will do this one

    hope this helps alittle

  4. #4
    Unregistered
    Guest
    Here's an analgy that might help.

    Say you have 80 pennies stretched out in a row. Each penny can be showing heads or tails. If you and I agree that if all the pennies show heads that means yes and if all the pennies show tails that means no, then you could leave me an answer to a message using the pennies as a form of memory.

    That type of messaging/memory is pretty crude however. So let's say you and I agree that we will use pennies in groups of 8. Each group of 8 can be arranged such that depending on the pattern of heads and tails appearing from left to right in the group of 8 it would stand for a letter. So, for example, if all 8 pennies are heads it stands for an A; and if only the farthest one to the right is tails it stands for a B; and if only the second one from the left in a given group of 8 is tails that stands for C and if both of the furthest right pennies in a group of 8 are tails that stands for a D. And so on. Using this type of message/memory system we can leave a message that had 10 different chars in it. And as long as nobody comes along and disturbs the message it will remain as long as needed, although you and I can change it whenever we wanted.

    How does that system of pennies relate to computer memory. Well, the smallest indivisible piece of memory in the penny system is one penny, whereas in the computer it's called a bit (exactly what a bit represents in phsycal terms depends on whether you are using tape memory, computer disc, pennys, or whatever). Each bit in the penny system can be only heads or tails, whereas in computer memory it is usually said that each bit can be either 1 or 0 (while that's not exactly true in terms of actual physical attributes, let's not get to technical here, okay). The next biggest chunk of memory in a computer is often called a byte. One byte is 8 bits. Since each bit in a byte can be a 0 or a 1, there can be 2^8 (or 256) different combinations of 0 and 1 in a byte. Rather than you and I agreeing on a system of what the patterns mean, in the computer world large groups of people have gotten together and agreed upon a system that correlates each one of the 256 combinations to a given char. The resulting character set usually has a name, like ASCII. It's easy to see now why the type char in C++ usually takes up 1 byte, or 8 bits of memory (the most prominent exception is unicode uses a two byte char, I think, so you do have to be a little careful). Unfortuntately, each compiler maker usually decides how much memory they are going to need to store an int, or a double, or a float; so they are not as standardized. The good news is that that's usually not to big a deal since there are only the three other primitive types.

  5. #5
    Unregistered
    Guest
    the null char is '\0'. It is a flag to the compiler that the input is a string rather than just a standard character array. assigning the integer 0 like this also indicates the null char, but use of the above form is less ambiguous.

    char dummy[1] = 0;
    char dummy2[3] = {'h', 'i', '\0'};

    NULL is defined as place in memory that is gauranteed to exist, but to which you can not assign a value or get the address of. It also can be indicated using the integer 0, although, agian, use of NULL is less ambiguous.

    int *ptr = NULL;
    int *ptr2 = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assistance with C Programming.
    By mintsmike in forum C Programming
    Replies: 10
    Last Post: 03-26-2009, 12:45 AM
  2. merging bits
    By Drac in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2008, 06:13 PM
  3. Arrays
    By spuk in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 11:40 AM
  4. Pointer array's and ASCII to bit conversion
    By AdamLAN in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2005, 06:55 PM
  5. Bits in files
    By robquigley in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2003, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21