logo

C の論理 NOT (!) 演算子

このセクションでは、C プログラミング言語の論理 NOT (!) 演算子について説明します。すでにご存知のとおり、論理演算子は、指定された式に対して 2 つ以上の条件を組み合わせて論理演算を実行するために使用されます。オペランドの論理条件が true の場合、演算子は true のブール値または 1 を返します。そうでない場合は、false のブール値または 0 を返します。論理演算子は、次の 3 つの部分に分類されます。 論理 AND、論理 OR、および論理 NOT 演算子。

C の論理 NOT (!) 演算子

論理AND演算子 指定された式内で残っている 2 つ以上のオペランドの条件が true であることを確認するために使用されます。 AND 演算子は、true または非ゼロ (1) の値を返します。それ以外の場合は、false または 0 の値を返します。したがって、論理 AND 演算子は、両方のオペランドの条件が true の場合にのみ式内で実行でき、いずれかの条件が true でない場合は 0 を返します。論理 AND 演算子は、2 つのアンパサンド '&&' 記号で表されます。

Javaでのカプセル化

構文:

 (A > b && b > c) 

論理OR演算子 は両方のオペランド (A と B) の条件をチェックするために使用され、オペランドまたは式のいずれかが true の場合、演算子は true のブール値を返します。同様に、どの式も true でない場合は、false またはゼロの値を返します。論理 OR 演算子は二重パイプ '||' として表されます。シンボル。

構文:

 (A &gt; B) || (A <c) < pre> <h3>Logical NOT operator</h3> <p>The logical NOT operator is represented as the &apos;!&apos; symbol, which is used to reverse the result of any given expression or condition. If the result of an expression is non-zero or true, the result will be reversed as zero or false value. Similarly, if the condition&apos;s result is false or 0, the NOT operator reverses the result and returns 1 or true.</p> <p>For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value. And if the user enters a zero (0) value, the operator returns the true Boolean value or 1. </p> <p> <strong>Syntax of the logical NOT operator</strong> </p> <pre> ! (condition); // It &apos;!&apos; represents the NOT operator </pre> <p>Here, the &apos;!&apos; symbol represents the logical NOT operator, which inverses the result of the given condition.</p> <h3>The truth table of the logical NOT operator:</h3> <p>Following is the truth table of the logical not operator in C</p> <pre> condition !(condition) 1 0 0 1 </pre> <h3>Example 1: Program to use the logical NOT operator in C</h3> <p>Let&apos;s create a simple program to reverse the given condition of the operands in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let&apos;s create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let&apos;s write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));></pre></c)>

ここで、「!」記号は、指定された条件の結果を反転する論理 NOT 演算子を表します。

論理 NOT 演算子の真理値表は次のとおりです。

以下は、C の論理 not 演算子の真理表です。

 condition !(condition) 1 0 0 1 

例 1: C で論理 NOT 演算子を使用するプログラム

C プログラミング言語でオペランドの指定された条件を逆にする簡単なプログラムを作成してみましょう。

 /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let&apos;s create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let&apos;s write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));>

上記のプログラムでは、NOT (!) 演算子を使用して、変数 x の条件が 5 に等しい、つまり true であるなど、さまざまな式の結果を逆にしています。それでも、NOT 演算子は結果を反転し、0 を返します。同様に、条件 (x!=5) を定義しましたが、論理演算子は結果を変更し、1 を返します。

例2: 数値を入力して論理否定演算を行うプログラム

C プログラミング言語の論理 NOT (!) 演算子を使用して、整数の逆の結果を取得する簡単なプログラムを作成してみましょう。

 /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } 

出力:

 Enter the number: 7 The result of x: 0 

上記のプログラムでは、ユーザーから整数値 7 を入力し、それを x 変数に格納します。その後、論理 NOT (!) 演算子は x の値 (ゼロ以外) を反転し、ゼロ (0) を返して x の結果を出力します。

2nd実行:

 Enter the number: 0 The result of x: 1 

同様に、ユーザーからゼロ (0) を入力し、論理 NOT (!) 演算子を使用して、x の値をゼロ以外の値 (1) に反転します。

オオカミ対キツネ

例 3: 論理 AND (&&)、OR (||)、および NOT (!) 演算子を使用してうるう年を検索するプログラム

C 言語の論理 AND (&&)、論理 OR (||)、および論理 NOT (!) 演算子を使用して、指定された年がうるうかどうかを確認する簡単なプログラムを作成してみましょう。

 #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } 

出力:

 Enter the year: 2020 2020 is a leap year. 

上記のプログラムでは、2020 と入力し、if...else ステートメントを定義して指定された年を確認します。このステートメントでは、2 つの条件を定義しました。

  1. 指定された年を 400 で割ると、0 に等しくなります。次に、論理 OR 演算子を使用して、左側または右側のオペランド条件が true であるかどうかを確認します。
  2. 2 番目の条件では、指定された年を 4 と 100 で割ります。しかし、2020 年を 4 で割ると、0 に等しくなります。同様に、2020 年を 100 で割りますが、これも 0 には等しくありません。つまり、両方とも「2020 年はうるう年」を表示する条件が true です。
  3. しかし、2021 年に入ると、「2021 年は閏年ではありません」という指定された結果が出力されます。

2nd実行:

.next java
 Enter the year: 2021 2021 is not a leap year. 

例 4: AND、OR、NOT 論理演算子を使用してさまざまな条件をチェックするプログラム

C で AND、OR、NOT 論理演算子を使用して、指定されたオペランドの複数の条件を示すプログラムを作成してみましょう。

 /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } 

出力:

 The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false.