{"id":26629,"date":"2022-04-02T20:12:38","date_gmt":"2022-04-02T14:42:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=26629"},"modified":"2022-04-02T20:12:38","modified_gmt":"2022-04-02T14:42:38","slug":"use-of-right-shift-and-left-shift","status":"publish","type":"post","link":"https:\/\/python-programs.com\/use-of-right-shift-and-left-shift\/","title":{"rendered":"Use of Right Shift “>>” and Left Shift “<<\" Operators in Python"},"content":{"rendered":"
In Python, they are Bitwise Operators known as Bitwise left shift(<<) and Bitwise right shift(>>).<\/p>\n
What are Operators?<\/strong><\/p>\n Operators are the special symbols used to do arithmetic and logical computations. Operators are used to alter values and variables. The value on which the operator operates is referred to as the Operand.<\/p>\n The shift operators are used to shift(move) the bits of a number to the left or right. The number is then multiplied or divided by two. In shifting operators, there are two types of shifting Processes.<\/p>\n The Bitwise Left shift shifts\/moves the bits of a number to the Left. We use the “left shift”(<<) symbol for this. It multiplies the number of bits by two respectively.<\/p>\n For example, let the number = 3<\/p>\n Its binary form = 0000 0011<\/p>\n 0000 0011<<1 bit = 0000 0110 = 6<\/p>\n Example1<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Bitwise Right Shift shifts\/moves the bits of a number to the right. We use the “right shift”(>>) symbol for this. It divides the number of bits by two respectively.<\/p>\n For example, let the number = 3<\/p>\n Its binary form = 0000 0011<\/p>\n 0000 0011>>1 bit = 0000 0001 = 1<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In Python, they are Bitwise Operators known as Bitwise left shift(<<) and Bitwise right shift(>>). What are Operators? Operators are the special symbols used to do arithmetic and logical computations. Operators are used to alter values and variables. The value on which the operator operates is referred to as the Operand. Python Shift Operators The …<\/p>\nPython Shift Operators<\/h2>\n
\n
Bitwise Left Shift<\/h3>\n
\n
# Give the number as static input and store it in a variable\r\ngvn_numb = 3\r\n# Left Shift 1 bit of the given number and print the result\r\nprint(\"The number after left shifting given number by 1 bit:\")\r\nprint(gvn_numb<<1)\r\n# Left Shift 2 bits of the given number and print the result\r\nprint(\"The number after left shifting given number by 2 bits:\")\r\nprint(gvn_numb<<2)<\/pre>\n
The number after left shifting given number by 1 bit:\r\n6\r\nThe number after left shifting given number by 2 bits:\r\n12<\/pre>\n
Bitwise Right Shift<\/h3>\n
\n
# Give the number as static input and store it in a variable\r\ngvn_numb = 3\r\n# Right Shift 1 bit of the given number and print the result\r\nprint(\"The number after Right shifting given number by 1 bit:\")\r\nprint(gvn_numb>>1)\r\n# Right Shift 2 bits of the given number and print the result\r\nprint(\"The number after Right shifting given number by 2 bits:\")\r\nprint(gvn_numb>>2)<\/pre>\n
The number after Right shifting given number by 1 bit:\r\n1\r\nThe number after Right shifting given number by 2 bits:\r\n0<\/pre>\n