Logical data type in computer science

There are a lot of data types in computer science. They are divided into reference types and value types. Value types usually work faster because they are stored on the stack and managed directly. These are numerical variables and logical data types. References store a link to the heap on the stack, and the type implementation is already on the heap. Reference data types are string variables, dynamic or global variables, built-in or user-defined classes.

Types of operations




On November 2, 1815, a man was born in England who became the most famous mathematician and logician, George Bull. It is in his honor that one of the data types in programming is named - Boolean, a logical data type.

George Bull




The boolean data type is a "primitive" type. It is ubiquitous: it was used in the programming language Algol 60 (1960s), Fortran, Pascal, C ++, is in all modern programming languages. A boolean variable is remarkable in that it has only two values: TRUE (true, 1) or FALSE (false, 0). It is usually implemented through a numerical data type, moreover, most programming languages ​​allow it to be used in arithmetic operations, having previously been converted to a numerical type based on the rules of type conversion for a particular programming language.





Low level

A boolean data type is a significant type. In RAM, variables of type bool are stored on the stack, that is, accessing and placing such a variable occurs very quickly.

It is easy to assume that 1 bit (0 or 1) is enough to implement such a simple type, but in fact, due to the architecture of the processors, the minimum addressable memory cell is reserved for the logical data type - byte, or, to be more precise, the machine word. This facilitates the operation of the processor and RAM. For instance:

00000000 - false

00000001 or 00010001 or 100000 - true

But when assigning the value TRUE to a Boolean variable, the value 00000001 is always written in machine code.

Logical data type. Logical operations

In common programming languages, the boolean type supports several operations; we will analyze the following:

  • LOGIC AND (AND && *).
  • LOGIC OR (OR | +).
  • EXCLUSIVE OR (XOR ^).
  • EQUALITY (EQV ==).
  • INCORRECT (NOT! =).
  • COMPARISON OPERATIONS (> <<= => =).

Boolean data types. Examples. Logical and

Truth table




The && operator returns true if both arguments are true.

using System; namespace Boolean { class MainClass { public static void Main(string[] args) { Console.WriteLine(true&&false); Console.ReadLine(); } } }
      
      



In this example, the second condition is FALSE, which means false is output to the console.





Logical OR

Operator || returns true if at least one of the arguments is true.

 using System; namespace Boolean { class MainClass { public static void Main(string[] args) { Console.WriteLine(true||false); Console.ReadLine(); } } }
      
      



In this example, one of the conditions is TRUE, which means that true will be displayed in the console.

Exclusive OR

The ^ operator (not to be confused with exponentiation) returns true if only one of the arguments is true.

 using System; namespace Boolean { class MainClass { public static void Main(string[] args) { Console.WriteLine(true^true); Console.ReadLine(); } } }
      
      



In this example, both arguments are true, which means false is output to the console.

Equality and Inequality

The == operator returns true if both conditions are equal. The! = Operator returns true if both conditions are not equal.

 using System; namespace Boolean { class MainClass { public static void Main(string[] args) { Console.WriteLine(true!=false); Console.ReadLine(); } } }
      
      



The console will be true because the condition is met.

Object Comparison




The above examples were in C #. In this language, two values ​​are defined for logical type operations - true and false. And here it is forbidden to convert Boolean values ​​to integer ones, the compiler will throw an error. It’s easy to note that when a Boolean variable is passed to the Console class in the WriteLine method, its value is output to the console.

Also, a boolean variable can control the if statement. If the condition for the expression with if to be true is the variable, then the expression can be reduced to:

 if(x) {/*...do smth...*/}
      
      



Comparison operations

The operators> = <=> <can use any numerical data types, including Boolean ones.

In C, if a variable contains 0, then its Boolean value is equivalent to FALSE, and vice versa, any number is treated as TRUE. For a better understanding below is a code snippet:

 if (bool_variable) printf("True!\n"); else printf("False!\n")
      
      



In C, there is no concept of a logical data type; instead, numeric types are used, mainly integer. Therefore, in logical operations, any operand of a numerical type that has a non-zero value is interpreted as TRUE, and the logical operations result in integers, one and zero, respectively. A logical data type was introduced in C ++, but rather for convenience. It does not affect the use of other types in logical expressions, as in C.

The order of execution of logical data type operators occurs from left to right. For example, evaluating an expression

 var x && var y && var z
      
      



will stop as soon as the first operand is detected in the value FALSE, and the calculation

 var x || var y || var z
      
      



will be executed until the compiler encounters the first true value.

Priority Operations




Pascal

In different programming languages, the subtleties of working with Boolean variables may differ slightly. The logical data type in Pascal, for example, has an interesting feature: here you can compare TRUE and FALSE, and FALSE is less than TRUE. Also, FALSE corresponds to zero, and TRUE to any number other than zero.

Access

The logical data type in Access works on similar principles.

  • LOGIC AND.
  • LOGIC OR.
  • EQUALITY Eqv.
  • INEQUALITY Not.
  • EXCLUSIVE OR Xor.

They are executed in the same way as in other popular programming languages.

Python

In Python, a logical data type is declared standard - bool. To cast other types to a Boolean type, the bool () function is used:

  • The empty line is FALSE, the non-empty line is TRUE.
  • Zero is FALSE, any number other than zero is TRUE.
  • An empty array is FALSE; a non-empty array is TRUE.
  • Function - TRUE.

These are all data types that can be cast to a boolean type in Python.

Ruby

FALSE means nil, and TRUE can be any object. Moreover, a variable with a value of 0 or an empty string is also considered TRUE, and this can cause difficulties for people starting to learn this language.

Logical data type operations may vary slightly depending on the language. Therefore, it is always necessary to get acquainted with the intricacies of using data types in the language being studied, so as not to have difficulties in further application.




All Articles