TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Shift Operators (Bitwise)

Use the shift operators. A shift moves all the bits right or left.
Shift. This operator moves bit positions. It changes the bit representation of a type. The bits are shifted right (or left) a number of positions. The C# language enables bitwise shifting with the right (>>) and left shift (<<) operators.
We introduce a program that shows the right shift (>>) and then left shift (<<) bitwise operators in the C# language. We repeatedly apply them and change the value of an int.

Info: The integer is shifted right (>>) zero places, then one place, and then two and more places.

Tip: You can see that all numbers that are negative have the very leftmost (first) bit set to 1, while positive numbers have it set to 0.

Note: The output of the program illustrates how the bit values are changed with the parameters to the shift operators.

C# program that uses right shift, left shift using System; class Program { static void Main() { // This program shift an integer right. // ... Then it shifts it left. // ... It displays the bits and the decimal representation. int value1 = 99999999; for (int i = 0; i < 32; i++) { int shift = value1 >> i; Console.WriteLine("{0} = {1}", GetIntBinaryString(shift), shift); } for (int i = 0; i < 32; i++) { int shift = value1 << i; Console.WriteLine("{0} = {1}", GetIntBinaryString(shift), shift); } } static string GetIntBinaryString(int value) { // From other article. return Convert.ToString(value, 2).PadLeft(32, '0'); } } Output 00000101111101011110000011111111 = 99999999 00000010111110101111000001111111 = 49999999 00000001011111010111100000111111 = 24999999 00000000101111101011110000011111 = 12499999 00000000010111110101111000001111 = 6249999 00000000001011111010111100000111 = 3124999 00000000000101111101011110000011 = 1562499 00000000000010111110101111000001 = 781249 00000000000001011111010111100000 = 390624 00000000000000101111101011110000 = 195312 00000000000000010111110101111000 = 97656 00000000000000001011111010111100 = 48828 00000000000000000101111101011110 = 24414 00000000000000000010111110101111 = 12207 00000000000000000001011111010111 = 6103 00000000000000000000101111101011 = 3051 00000000000000000000010111110101 = 1525 00000000000000000000001011111010 = 762 00000000000000000000000101111101 = 381 00000000000000000000000010111110 = 190 00000000000000000000000001011111 = 95 00000000000000000000000000101111 = 47 00000000000000000000000000010111 = 23 00000000000000000000000000001011 = 11 00000000000000000000000000000101 = 5 00000000000000000000000000000010 = 2 00000000000000000000000000000001 = 1 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000000000000000000000000000000 = 0 00000101111101011110000011111111 = 99999999 00001011111010111100000111111110 = 199999998 00010111110101111000001111111100 = 399999996 00101111101011110000011111111000 = 799999992 01011111010111100000111111110000 = 1599999984 10111110101111000001111111100000 = -1094967328 01111101011110000011111111000000 = 2105032640 11111010111100000111111110000000 = -84902016 11110101111000001111111100000000 = -169804032 11101011110000011111111000000000 = -339608064 11010111100000111111110000000000 = -679216128 10101111000001111111100000000000 = -1358432256 01011110000011111111000000000000 = 1578102784 10111100000111111110000000000000 = -1138761728 01111000001111111100000000000000 = 2017443840 11110000011111111000000000000000 = -260079616 11100000111111110000000000000000 = -520159232 11000001111111100000000000000000 = -1040318464 10000011111111000000000000000000 = -2080636928 00000111111110000000000000000000 = 133693440 00001111111100000000000000000000 = 267386880 00011111111000000000000000000000 = 534773760 00111111110000000000000000000000 = 1069547520 01111111100000000000000000000000 = 2139095040 11111111000000000000000000000000 = -16777216 11111110000000000000000000000000 = -33554432 11111100000000000000000000000000 = -67108864 11111000000000000000000000000000 = -134217728 11110000000000000000000000000000 = -268435456 11100000000000000000000000000000 = -536870912 11000000000000000000000000000000 = -1073741824 10000000000000000000000000000000 = -2147483648
Uses. Each time you use the Dictionary or Hashtable collection or call the GetHashCode method on a string, shift operators are used to acquire the hash code. You can use a right shift to implement divide by two on positive integral types.

Tip: If you are using a data structure that uses bitmasks, bit shifting can be used to guide the control flow.

GetHashCodeDivide by Powers of Two
Binary operators. The term "binary operators" is used to describe operators that receive two parameters. The term "bitwise operator" indicates an operator that receives one or two operands and is meant to change the bit representation of an integer.
The term "unary operator" is used to indicate an operator such as unary negation that receives only one argument. It is the opposite of "binary operator" and not bitwise operator.
Summary. We explored the shift operator and how it can be applied to change the value of an integer. The article provides a program to illustrate this clearly through a simulation.Binary Representation
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf