User Tools

Site Tools


haas:fall2022:common:templates:experiment

Experiment 1

Premise/Question

In logic, we evaluate conditions based on true and false values. In electricity, these are represented as voltage levels. On the computer, we must represent them as a numeric value.

Many examples set the value of 1 to represent true, and 0 to represent false. However, in C, when we exit the program and desire to send an EXIT_SUCCESS, we return a 0, which would make it seem that 0 is true.

The question is: What is the numeric value of true and false on the computer in the C programming language?

Resources

The following resources provide some background information on truth values for logic:

One needs to be mindful of the situation that the “values” assigned for true and false are largely arbitrary. They are constant within the scope of a particular environment, but not universal to all environments. We assign a value to associate with truth values, and must be exercise appropriate awareness.

Hypothesis

Knowing that many electronic components are based on the notion of “negative enable”, and that to return a true value from our C programs, we “return(0);”, I believe that true values in C are represented by a 0 and false values by a 1.

Experiment

To test true or false values, we must perform a comparison. In C, common comparison operations are performed by if() statements and loops (for(), while(), and do-while()), so any experiment validating the actual numeric value of true and false must involve them.

An assumption that will be made:

  • the value of true/false will be the same for if, for, while, and do-while. It would not make sense for these values to be different since they share so much similarity in grammar and usage.

Additionally, since we are making use of logic, we should also test our hypothesis against values processed by C logical operators (AND, OR, NOT). There are two logical situations that will work to our advantage:

  • in an AND evaluation, differing inputs result in a false result. If we AND 1 and 0, the result will reflect the false value.
  • in an OR evaluation, differing inputs result in a true result. If we OR 1 and 0, the result will reflect the true value.

To test my hypothesis, I have developed the following program:

/*
 * logictest.c - an experiment to determine the value of true and false in C
 *
 * written by: Matthew Haas
 * course: CSCS1320 C/C++ Programming
 * semester: Fall 2011
 * 
 * to compile: gcc -o logictest logictest.c
 * to execute: ./logictest
 */
#include <stdio.h>
 
int main()
{
    // Declare some variables for testing
    int a = 0; 
    int b = 1;
    int c = 2; // why 2? It'll be interesting to see what the result is.
    int d = 0;
    int e = 0;
 
    // Round one: Perform a comparison, output the result
    if (a)
    {
        printf("a: %d is true\n", a);
    }
    else
    {
        printf("a: %d is false\n", a);
    }
 
    // Round two: Perform a comparison, output the result
    if (b)
    {
        printf("b: %d is true\n", b);
    }
    else
    {
        printf("b: %d is false\n", b);
    }
 
    // Round three: Perform a comparison, output the result
    if (c)
    {
        printf("c: %d is true\n", c);
    }
    else
    {
        printf("c: %d is false\n", c);
    }
 
    // Round four: Perform a logical AND test, output the result
    d = a & b;
    printf("a AND b is: %d\n", d);
 
    // Round five: Perform a logical OR test, output the result
    e = a | b;
    printf("a OR b is: %d\n", e);
 
    return(0);
}

Because if statements evaluate based on the result of the comparison (ie the resulting true or false value), our entire expression within the if will be the variable (that's it). Running this program should shed some light on the value of truth.

Data

Upon compiling and executing the above code, the output is as follows:

lab46:~/src/cprog/experiments$ ./logict 
a: 0 is false
b: 1 is true
c: 2 is true
a AND b is: 0
a OR b is: 1
lab46:~/src/cprog/experiments$ 

Analysis

Based on the data collected:

  • my stated hypothesis appears to be incorrect- instead a value of 0 appears to be false, and 1 and 2 ring as true.
  • something is still unresolved… why do we signal success by return(0); and failure by non-zero?
  • the scope of the experiment helped proved the values of true and false within C, but there may still be other environments to consider (outside the program in the operating system).
  • aside from testing more values, it would appear that 0 is false, and non-zero is true. Additional experimentation can help determine if all non-zero are true (for example, what about negative values?)

Conclusions

As a result of the experiment, we can make the claim that in C evaluation statements and logical operators work on the premise that 0 represents false, and non-zero represents true. While this situation exists within the processing environment of C, there is still question of what represents true and false outside of C, such as in the operating system.

haas/fall2022/common/templates/experiment.txt · Last modified: 2011/06/07 14:57 by 127.0.0.1