Boolean NumbersDate: 04/01/2003 at 02:50:53 From: Sakshi Subject: Boolean Data Types - True and False Numbers I am doing an introduction to C-Programming course. We learned Boolean data types so we could have variable declarations while writing a program. The course said a Boolean number was a number that was either true or false. I'm not sure what a true or false number is. Date: 04/01/2003 at 13:13:39 From: Doctor Peterson Subject: Re: Boolean Data Types - True and False Numbers Hi, Sakshi. It's easy to misunderstand this concept if you are not familiar with the idea of Boolean algebra first. You've sort of got it backwards; it's not that a number can be true or false, but that we can represent truth or falsity by a number. Boolean algebra is a way of talking about logic as if it were algebra. Rather than numbers, we use the two values True and False, and instead of addition and subtraction, we have operations like And and Or. For example, "True Or False" is an expression whose value is True. Why? Because the Or operation tells us if either the first OR the second operand is true; in this case the first is, so that answer is True. Boolean algebra is very useful in computers, because you want to be able to tell whether some statement (such as "x == 5") is true or false, and then do different things based on the result. But you need some way to represent True and False in the program. In C, the solution is to use a number (integer). The number zero is taken to mean False, and any other number is taken to mean True. That allows you to say things like this: if ((x == 5) || (y < 4)) ... where "||" is the C symbol for Or. If x is 3 and y is 2, then (x == 5) has the value False (0) because 3 is not equal to 5 (y < 4) has the value True (1) because 2 is less than 4 so when we "Or" these together, the answer is True, and we will do the following actions. The funny thing is that in a computer, at the most basic level, everything is represented by nothing but True and False, 1 and 0; numbers are represented in binary using those bits; but then C represents True and False in terms of integers. That's because C wants to deal only with numbers at a level all computers will understand (bytes), rather than with bits, which are generally harder to work with. Here is an explanation of Boolean algebra in a little more depth: Computers: Defining Logical Operations http://mathforum.org/library/drmath/view/54335.html If you have any further questions, feel free to write back. - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/ Date: 04/02/2003 at 03:32:44 From: Sakshi Subject: Thank you (Boolean Data Types - True and False Numbers) Thanks! That helped a lot. So it is basically whether the numerical value of a variable works out which is true or false. Date: 04/02/2003 at 08:35:43 From: Doctor Peterson Subject: Re: Thank you (Boolean Data Types - True and False Numbers) Hi, Sakshi. Right. Operations that return a Boolean value return 1 for True and 0 for False; operations that expect a Boolean value take anything other than zero to mean True. Other programming languages will have different ways of doing the same thing. C++ has a special Boolean data type. - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/ |
Search the Dr. Math Library: |
[Privacy Policy] [Terms of Use]
Ask Dr. Math^{TM}
© 1994- The Math Forum at NCTM. All rights reserved.
http://mathforum.org/dr.math/