{"id":179,"date":"2020-08-29T23:27:11","date_gmt":"2020-08-29T23:27:11","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=179"},"modified":"2020-08-29T23:27:11","modified_gmt":"2020-08-29T23:27:11","slug":"c-operators","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-operators\/","title":{"rendered":"C++ Operators"},"content":{"rendered":"<p>In this tutorial, you will learn about the various operators available in C++. An operator is a symbol that tell the compiler to perform certain operation on variables. Let&#8217;s consider the following six categories of operators:<\/p>\n<ol class=\"list\">\n<li><a href=\"#t1\">Arithmetic Operators<\/a><\/li>\n<li><a href=\"#t2\">Relational Operators<\/a><\/li>\n<li><a href=\"#t3\">Logical Operators<\/a><\/li>\n<li><a href=\"#t4\">Bitwise Operators<\/a><\/li>\n<li><a href=\"#t5\">Assignment Operators<\/a><\/li>\n<li><a href=\"#t6\">Other Operators<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Arithmetic Operators<\/strong><\/h4>\n<p>These are operators used to perform arithmetic operations. For example if x = 10 and y = 20, then the table below show the results of various arithmetic operators<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"10%\">Operator<\/th>\n<th width=\"45%\">Brief Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>Addition (+)<\/td>\n<td>Adds two operands<\/td>\n<td>x + y will give 30<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Subtraction(-)<\/td>\n<td>Subtracts second operand from the first<\/td>\n<td class=\"ts\">x &#8211; y will give -10<\/td>\n<\/tr>\n<tr>\n<td>Multiplication(*)<\/td>\n<td>Multiplies both operands<\/td>\n<td>x * y will give 200<\/td>\n<\/tr>\n<tr>\n<td>Division(\/)<\/td>\n<td>Divides numerator by de-numerator<\/td>\n<td>y \/ x will give 2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Modulus(%)<\/td>\n<td>Gives remainder of after an integer division<\/td>\n<td class=\"ts\">y % x will give 0<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Increment(++)<\/td>\n<td>Increases integer value by one<\/td>\n<td class=\"ts\">x++ will give 11<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Decrement(&#8211;)<\/td>\n<td>Decreases integer value by one<\/td>\n<td class=\"ts\">x&#8211; will give 9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Relational Operators<\/strong><\/h4>\n<p>These are sometimes called comparison operators. They compare two operand and returns either true or false based on the result. So if x = 10 and y = 10, then the table below show the results of various relational operators<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"10%\">Operator<\/th>\n<th width=\"45%\">Brief Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">==<\/td>\n<td>Checks whether the values of two operands are equal, if yes then the condition becomes true.<\/td>\n<td class=\"ts\">(x == y) is not true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">!=<\/td>\n<td>Checks whether the values of two operands are equal, if values are not equal then the condition becomes true.<\/td>\n<td class=\"ts\">(x != y) is true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&gt;<\/td>\n<td>Checks whether the value of left operand is greater than the value of right operand, if so, then the condition becomes true.<\/td>\n<td class=\"ts\">(x &gt; y) is not true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&lt;<\/td>\n<td>Checks whether the value of left operand is less than the value of right operand, if so, then the condition becomes true.<\/td>\n<td class=\"ts\">(x &lt; y) is true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&gt;=<\/td>\n<td>Checks whether the value of left operand is greater than or equal to the value of right operand, if so then the condition becomes true.<\/td>\n<td class=\"ts\">(x &gt;= y) is not true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&lt;=<\/td>\n<td>Checks whether the value of left operand is less than or equal to the value of right operand, if yes then the condition becomes true.<\/td>\n<td class=\"ts\">(x &lt;= y) is true.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Logical Operators<\/strong><\/h4>\n<p>These are used to do logical comparison of two operands. If x = 1 and y = 0, then:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"10%\">Operator<\/th>\n<th width=\"45%\">Brief Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">Logical AND(&amp;&amp;)<\/td>\n<td>If both of the operands are non-zero, then the condition becomes true.<\/td>\n<td class=\"ts\">(x &amp;&amp; y) is false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Logical OR(||)<\/td>\n<td>If any of the two operands is non-zero, then condition evaluates to true.<\/td>\n<td class=\"ts\">(x || y) is true.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">:Logical NOT(!)<\/td>\n<td>Use to negate the logical state of its operand. If a condition is true, then the operator will make it false.<\/td>\n<td class=\"ts\">!(x &amp;&amp; y) is true.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Bitwise Operators<\/strong><\/h4>\n<p>These set of operators are used perform operations on individual bits. They are also called binary operators or bit-by-bit operators.<\/p>\n<p>For instance, let x = 60; and y = 13; converting to\u00a0 binary format they will be as follows \u2212<\/p>\n<p>x = 0011 1100<\/p>\n<p>y = 0000 1101<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>x &amp; y = 0000 1100<\/p>\n<p>x | y = 0011 1101<\/p>\n<p>x ^ y = 0011 0001<\/p>\n<p>~x\u00a0 = 1100 0011<\/p>\n<p>The table below summarizes bitwise operators.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"10%\">Operator<\/th>\n<th width=\"45%\">Brief Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary AND(&amp;)<\/td>\n<td>copies a bit to the result if it exists in both operands.<\/td>\n<td>(x &amp; y) will give 12 which is 0000 1100<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary OR(|)<\/td>\n<td>copies a bit if it exists in either operand.<\/td>\n<td>(x | y) will give 61 which is 0011 1101<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary XOR(^)<\/td>\n<td>copies the bit if it is set in one operand but not both.<\/td>\n<td>(x ^ y) will give 49 which is 0011 0001<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary 1s complement(~)<\/td>\n<td>a unary and has the effect of &#8216;flipping&#8217; bits.<\/td>\n<td>(~x ) will give -61 which is 1100 0011 in 2&#8217;s complement form due to a signed binary number.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary Left Shift(&lt;&lt;)<\/td>\n<td>shifts the left operands value left by the number of bits specified by the right operand.<\/td>\n<td>x &lt;&lt; 2 will give 240 which is 1111 0000<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Binary Right Shift(&gt;&gt;)<\/td>\n<td>shifts the left operands value right by the number of bits specified by the right operand.<\/td>\n<td>x &gt;&gt; 2 will give 15 which is 0000 1111<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Assignment Operators<\/strong><\/h4>\n<p>These are simply used to assign values to variables. Assignment operators are summarized in the table below<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"10%\">Operator<\/th>\n<th width=\"48%\">Brief Description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">Simple Assignment(=)<\/td>\n<td>assigns values from right side operands to left side operand.<\/td>\n<td>z = x + y will assign value of x + y into z<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Add AND Assignment(+=)<\/td>\n<td>adds right operand to the left operand and assign the result to left operand.<\/td>\n<td>z += x is equivalent to z = z + x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Subtract AND Assignment(-=)<\/td>\n<td>subtracts right operand from the left operand and assign the result to left operand.<\/td>\n<td>z -= x is equivalent to z = z &#8211; x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Multiply AND Assignment(*=)<\/td>\n<td>multiplies right operand with the left operand and assign the result to left operand.<\/td>\n<td>z *= x is equivalent to z = z * x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Divide AND Assignment(\/=)<\/td>\n<td>divides left operand with the right operand and assign the result to left operand.<\/td>\n<td>z \/= x is equivalent to z = z \/ x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Modulus AND Assignment(%=)<\/td>\n<td>\u00a0takes modulus using two operands and assign the result to left operand.<\/td>\n<td>z %= x is equivalent to z = z % x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Left Shift AND Assignment(&lt;&lt;=)<\/td>\n<td><\/td>\n<td>z &lt;&lt;= 2 is same as z = z &lt;&lt; 2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Right Shift AND Assignment(&gt;&gt;=)<\/td>\n<td><\/td>\n<td>z &gt;&gt;= 2 is same as z = z &gt;&gt; 2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Bitwise AND Assignment(&amp;=)<\/td>\n<td><\/td>\n<td>z &amp;= 2 is same as z = z &amp; 2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Bitwise XOR Assignment(^=)<\/td>\n<td><\/td>\n<td>z ^= 2 is same as z = z ^ 2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">Bitwise OR Assignment(|=)<\/td>\n<td><\/td>\n<td>z |= 2 is same as z = z | 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Other Operators<\/strong><\/h4>\n<p>There are\u00a0 a few other operators which does not fall under any of the categories discussed. So we group them as &#8216;other operators&#8217; or &#8216;misc operators&#8217;. They are listed in the table below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Operator\u00a0 and Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>sizeof operator<\/b><\/p>\n<p>returns the size of a variable. For instance, sizeof(x), where \u2018x\u2019 is integer, and will return 4.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>Conditional operator(a ? b : c<\/b><\/p>\n<p>If a is true then it returns value of b else, returns value of c<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>Comma (,)<\/b><\/p>\n<p>causes a sequence of operations to be performed. The value of the entire expression becomes the value of the last expression of the comma-separated list.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>Member operators . (dot) and -&gt; (arrow)<\/b><\/p>\n<p>they are used to reference individual members of classes, structures, and unions.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>Casting operators<\/b><\/p>\n<p>converts one data type to another. For instance, int(12.75) would return 12. <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-type-conversion\/\" target=\"_blank\" rel=\"noopener noreferrer\">See Data Type Conversion<\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>Pointer operator &amp;<\/b><\/p>\n<p>returns the address of a variable. For instance &amp;a; will give actual address of the variable.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>Pointer operator *<\/b><\/p>\n<p>* is pointer to a variable. For instance *var; will pointer to a variable var.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the various operators available in C++. An operator is a symbol that tell the compiler to perform certain &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/179\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}