12,892,320 members (48,843 online)
alternative version

#### Stats

251.6K views
36 bookmarked
Posted 6 Nov 2001

# Bit wise operations in C#

, 6 Nov 2001
 Rate this:
Using bitwise operators in C#

## Introduction

C# has lots of flexibility over manipulating with bits. Before I start explaining about bit wise manipulation I would like to give some inputs on binary operations.

## Binary numbers

With only two symbols you can represent any type of information you want, these symbols can be {a,b}, {0,1} or the {beep, beeeep} of the Morse code. When you want to work with boolean (1) expressions or place multiple values in a single byte (group of 8 bit), it is more convenient for you to represent these bytes as binary numbers.

Binary numbers are specifically required to build bit-masks, used with boolean operators (AND, OR, XOR, NOT). In other words, 235 is the addition of 128+64+32+8+2+1. Binary numbers seem to be very long numbers, but they are much easier for a computer to handle since each bit, or binary digit can be represented by an electrical signal which is either on of off

 128 64 32 16 8 4 2 1 1 1 0 1 0

Using the above table you can see that the decimal number 11010 is equal to 26 in the decimal system. (16+8+2=26) - Use the base checker at the top to turn 26 into binary if you want to check.

Using binary notation is a very good exercise for Numerical Hour since the principles can be quickly taught - also some very interesting math’s can be done quickly - e.g.

To half any number - simply move the digits 1 place to the right: 101100 = 44 , 10110=22
(what happens if their is a fractional part - Can you make a rule?)

To double a number - simply add a zero on the end: 1111 = 15, 11110 = 30

Some of the important binary operations, which I am going to discuss, are following:

• AND operation
• OR operation
• Shift operations

As we know in binary all 1’s are true and 0’s are considered to be false.

When AND operations is done on the binary value following are the results of AND.

Following is the truth table for AND operation.

 A B AND 0 (false) 0(false) 0 (false) 1 (True) 0(false) 0 (false) 0(false) 1(True) 0 (false) 1(True) 1(True) 1 (True)

*When using AND operation it gives True only when both the values are True.

In C# to implement the AND operation using ‘&’ Operator.

Now let's see e first program

## Program 1

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
byte varB=20;// binary equivalent for 20 is 10100
long result=varA & varB; // AND operation result should be 00000
Console.WriteLine("{0}  AND  {1} Result :{2}",varA,varB,result);

varA=10;// binary equivalent for 10 is 01010
varB=10;// binary equivalent for 10 is 01010
result=varA & varB; // AND operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("{0}  AND  {1} Result : {2}",varA,varB,result);

}
}```

### Program Output:

```C:\csharp\progs>bitprg1
10  AND  20 Result :0
10  AND  10 Result : 10  ```

When OR operations is done on the binary value following are the results of OR.

Following is the truth table for AND operation.

 A B OR 0 (false) 0(false) 0 (false) 1 (True) 0(false) 1 (True) 0(false) 1(True) 1 (True) 1(True) 1(True) 1 (True)

*When using OR operation it gives FALSE only when both the values are FALSE. In all other cases OR operation gives true.

In C# to implement the OR operation using ‘|’ Operator.

Now let's see e first program

## Program 2

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
byte varB=20;// binary equivalent for 20 is 10100
long result=varA | varB; // OR operation result should be 11110
//so the result will contain 30 in decimal
Console.WriteLine("{0}  OR  {1} Result :{2}",varA,varB,result);

varA=10;// binary equivalent for 10 is 01010
varB=10;// binary equivalent for 10 is 01010
result=varA | varB; // OR operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("{0}  OR  {1} Result : {2}",varA,varB,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg2
10  OR  20 Result :30
10  OR  10 Result : 10  ```

There are two kinds of Shift operations on Right Shift and Left Shift.

• Right Shift operation is used for shifting the bits positions towards right side.
• Left Shift operation is used for shifting the bits positions towards left side.

When RightShift operations are done on a binary value the bits are shifted to one position towards right side.

Let’s take a example:

The binary equivalent for the decimal value 10 is 1010.So when Right Shift operation is done this value. The all the bits will move one position towards right so the right most bits will be truncated and left most bits is filled with zero.

1010 when shifted to right one position its value will be 0101

So the decimal equivalent for 0101 is 5. This means when decimal value 10 shifted to right one position its value is reduced to 5.

In C# to implement the Right shift operation using ‘>>’ Operator.

Now let's see e first program

## Program 3

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
long result=varA >> 1; // Right Shift operation result should be 0101
//so the result will contain 5 in decimal
Console.WriteLine("{0} is Right Shifted to 1 position Result :{1}",
varA,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg3
10 is Right Shifted to 1 position Result :5```

When LeftShift operations are done on a binary value the bits are shifted to one position towards left side.

Let’s take an example:

The binary equivalent for the decimal value 10 is 1010.

So when left Shift operation is done this value. The all the bits will move one position towards left so the left most bit will be truncated and right most bit is filled with zero.1010 when shifted to right one positions its value will be 10100.

So the decimal equivalent for 10100 is 20. This means when decimal value 10 shifted to left one position its value is increased to 20.

In C# to implement the Left shift operation using ‘<<’ Operator.

Now let's see e first program

## Program 4

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
long result=varA << 1; // Left Shift operation result should be 10100
//so the result will contain 20 in decimal
Console.WriteLine("{0} is Left Shifted to 1 position Result :{1}",
varA,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg4
10 is Left Shifted to 1 position Result :20```

A list of licenses authors might use can be found here

## Share

 Architect United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

## You may also be interested in...

 First PrevNext
 I want to group bits of data into byte in c# Member 1142340423-Feb-15 0:09 Member 11423404 23-Feb-15 0:09
 My vote of 5 rtz874-Feb-14 23:33 rtz87 4-Feb-14 23:33
 My vote of 5 Innocent9109-Sep-13 1:24 Innocent910 9-Sep-13 1:24
 My vote of 5 Terry4729720-Jun-13 17:38 Terry47297 20-Jun-13 17:38
 My vote of 3 Guziec8717-Jun-13 9:07 Guziec87 17-Jun-13 9:07
 My vote of 5 juxrummy28-Mar-13 5:15 juxrummy 28-Mar-13 5:15
 This info was really helpfull for me yytg1-Nov-12 5:02 yytg 1-Nov-12 5:02
 Hi could you have a look at this nik05070617-Jun-08 8:12 nik050706 17-Jun-08 8:12
 how to write these individual bits to a file?? appliprog16-Oct-05 8:51 appliprog 16-Oct-05 8:51
 Re: how to write these individual bits to a file?? Scott Bruno5-Apr-14 18:43 Scott Bruno 5-Apr-14 18:43
 Re: How about display in binary... MSA54200018-May-06 3:49 MSA542000 18-May-06 3:49
 pigeon english Anonymous29-Nov-04 3:18 Anonymous 29-Nov-04 3:18
 Re: pigeon english Anonymous18-Mar-05 20:33 Anonymous 18-Mar-05 20:33
 Re: pigeon english sdfgasdfsfgsdfg18-Mar-07 16:31 sdfgasdfsfgsdfg 18-Mar-07 16:31
 Add extra serup6-Jul-04 1:27 serup 6-Jul-04 1:27
 Bitwise operations in C# VoidPointer8-Apr-04 12:04 VoidPointer 8-Apr-04 12:04
 What next? Tomasz Sowinski7-Nov-01 8:00 Tomasz Sowinski 7-Nov-01 8:00
 Re: What next? Nemanja Trifunovic7-Nov-01 8:22 Nemanja Trifunovic 7-Nov-01 8:22
 Re: What next? [modified] Fazlul Kabir7-Nov-01 8:31 Fazlul Kabir 7-Nov-01 8:31
 Re: What next? Tomasz Sowinski7-Nov-01 8:38 Tomasz Sowinski 7-Nov-01 8:38
 Re: What next? Anonymous20-Oct-02 10:17 Anonymous 20-Oct-02 10:17
 Re: What next? Anonymous20-Apr-05 6:13 Anonymous 20-Apr-05 6:13
 Of Power Switches and Social Ineptitude Software_Architect9-May-05 19:23 Software_Architect 9-May-05 19:23
 Re: What next? Anonymous25-Mar-02 21:13 Anonymous 25-Mar-02 21:13
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Apr-17 10:24 Refresh 12 Next »
The text passage provided is a technical article that explains bitwise operations in C#. It covers binary numbers, AND, OR, and shift operations, and includes example programs demonstrating these concepts.
no_question

the passage does not contain any explicit mathematical questions or problems to solve. it is primarily an explanatory article on bitwise operations in c#. while it does include some examples with numerical values, these are used to illustrate the concepts rather than posing questions for the reader to solve.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
12,892,320 members (48,843 online)
alternative version

#### Stats

251.6K views
36 bookmarked
Posted 6 Nov 2001

# Bit wise operations in C#

, 6 Nov 2001
 Rate this:
Using bitwise operators in C#

## Introduction

C# has lots of flexibility over manipulating with bits. Before I start explaining about bit wise manipulation I would like to give some inputs on binary operations.

## Binary numbers

With only two symbols you can represent any type of information you want, these symbols can be {a,b}, {0,1} or the {beep, beeeep} of the Morse code. When you want to work with boolean (1) expressions or place multiple values in a single byte (group of 8 bit), it is more convenient for you to represent these bytes as binary numbers.

Binary numbers are specifically required to build bit-masks, used with boolean operators (AND, OR, XOR, NOT). In other words, 235 is the addition of 128+64+32+8+2+1. Binary numbers seem to be very long numbers, but they are much easier for a computer to handle since each bit, or binary digit can be represented by an electrical signal which is either on of off

 128 64 32 16 8 4 2 1 1 1 0 1 0

Using the above table you can see that the decimal number 11010 is equal to 26 in the decimal system. (16+8+2=26) - Use the base checker at the top to turn 26 into binary if you want to check.

Using binary notation is a very good exercise for Numerical Hour since the principles can be quickly taught - also some very interesting math’s can be done quickly - e.g.

To half any number - simply move the digits 1 place to the right: 101100 = 44 , 10110=22
(what happens if their is a fractional part - Can you make a rule?)

To double a number - simply add a zero on the end: 1111 = 15, 11110 = 30

Some of the important binary operations, which I am going to discuss, are following:

• AND operation
• OR operation
• Shift operations

As we know in binary all 1’s are true and 0’s are considered to be false.

When AND operations is done on the binary value following are the results of AND.

Following is the truth table for AND operation.

 A B AND 0 (false) 0(false) 0 (false) 1 (True) 0(false) 0 (false) 0(false) 1(True) 0 (false) 1(True) 1(True) 1 (True)

*When using AND operation it gives True only when both the values are True.

In C# to implement the AND operation using ‘&’ Operator.

Now let's see e first program

## Program 1

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
byte varB=20;// binary equivalent for 20 is 10100
long result=varA & varB; // AND operation result should be 00000
Console.WriteLine("{0}  AND  {1} Result :{2}",varA,varB,result);

varA=10;// binary equivalent for 10 is 01010
varB=10;// binary equivalent for 10 is 01010
result=varA & varB; // AND operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("{0}  AND  {1} Result : {2}",varA,varB,result);

}
}```

### Program Output:

```C:\csharp\progs>bitprg1
10  AND  20 Result :0
10  AND  10 Result : 10  ```

When OR operations is done on the binary value following are the results of OR.

Following is the truth table for AND operation.

 A B OR 0 (false) 0(false) 0 (false) 1 (True) 0(false) 1 (True) 0(false) 1(True) 1 (True) 1(True) 1(True) 1 (True)

*When using OR operation it gives FALSE only when both the values are FALSE. In all other cases OR operation gives true.

In C# to implement the OR operation using ‘|’ Operator.

Now let's see e first program

## Program 2

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
byte varB=20;// binary equivalent for 20 is 10100
long result=varA | varB; // OR operation result should be 11110
//so the result will contain 30 in decimal
Console.WriteLine("{0}  OR  {1} Result :{2}",varA,varB,result);

varA=10;// binary equivalent for 10 is 01010
varB=10;// binary equivalent for 10 is 01010
result=varA | varB; // OR operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("{0}  OR  {1} Result : {2}",varA,varB,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg2
10  OR  20 Result :30
10  OR  10 Result : 10  ```

There are two kinds of Shift operations on Right Shift and Left Shift.

• Right Shift operation is used for shifting the bits positions towards right side.
• Left Shift operation is used for shifting the bits positions towards left side.

When RightShift operations are done on a binary value the bits are shifted to one position towards right side.

Let’s take a example:

The binary equivalent for the decimal value 10 is 1010.So when Right Shift operation is done this value. The all the bits will move one position towards right so the right most bits will be truncated and left most bits is filled with zero.

1010 when shifted to right one position its value will be 0101

So the decimal equivalent for 0101 is 5. This means when decimal value 10 shifted to right one position its value is reduced to 5.

In C# to implement the Right shift operation using ‘>>’ Operator.

Now let's see e first program

## Program 3

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
long result=varA >> 1; // Right Shift operation result should be 0101
//so the result will contain 5 in decimal
Console.WriteLine("{0} is Right Shifted to 1 position Result :{1}",
varA,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg3
10 is Right Shifted to 1 position Result :5```

When LeftShift operations are done on a binary value the bits are shifted to one position towards left side.

Let’s take an example:

The binary equivalent for the decimal value 10 is 1010.

So when left Shift operation is done this value. The all the bits will move one position towards left so the left most bit will be truncated and right most bit is filled with zero.1010 when shifted to right one positions its value will be 10100.

So the decimal equivalent for 10100 is 20. This means when decimal value 10 shifted to left one position its value is increased to 20.

In C# to implement the Left shift operation using ‘<<’ Operator.

Now let's see e first program

## Program 4

```using System;

class MyClass {
public static void Main() {

byte varA=10;// binary equivalent for 10 is 01010
long result=varA << 1; // Left Shift operation result should be 10100
//so the result will contain 20 in decimal
Console.WriteLine("{0} is Left Shifted to 1 position Result :{1}",
varA,result);

}
}```

### Program output:

```C:\csharp\progs>bitprg4
10 is Left Shifted to 1 position Result :20```

A list of licenses authors might use can be found here

## Share

 Architect United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

## You may also be interested in...

 First PrevNext
 I want to group bits of data into byte in c# Member 1142340423-Feb-15 0:09 Member 11423404 23-Feb-15 0:09
 My vote of 5 rtz874-Feb-14 23:33 rtz87 4-Feb-14 23:33
 My vote of 5 Innocent9109-Sep-13 1:24 Innocent910 9-Sep-13 1:24
 My vote of 5 Terry4729720-Jun-13 17:38 Terry47297 20-Jun-13 17:38
 My vote of 3 Guziec8717-Jun-13 9:07 Guziec87 17-Jun-13 9:07
 My vote of 5 juxrummy28-Mar-13 5:15 juxrummy 28-Mar-13 5:15
 This info was really helpfull for me yytg1-Nov-12 5:02 yytg 1-Nov-12 5:02
 Hi could you have a look at this nik05070617-Jun-08 8:12 nik050706 17-Jun-08 8:12
 how to write these individual bits to a file?? appliprog16-Oct-05 8:51 appliprog 16-Oct-05 8:51
 Re: how to write these individual bits to a file?? Scott Bruno5-Apr-14 18:43 Scott Bruno 5-Apr-14 18:43
 Re: How about display in binary... MSA54200018-May-06 3:49 MSA542000 18-May-06 3:49
 pigeon english Anonymous29-Nov-04 3:18 Anonymous 29-Nov-04 3:18
 Re: pigeon english Anonymous18-Mar-05 20:33 Anonymous 18-Mar-05 20:33
 Re: pigeon english sdfgasdfsfgsdfg18-Mar-07 16:31 sdfgasdfsfgsdfg 18-Mar-07 16:31
 Add extra serup6-Jul-04 1:27 serup 6-Jul-04 1:27
 Bitwise operations in C# VoidPointer8-Apr-04 12:04 VoidPointer 8-Apr-04 12:04
 What next? Tomasz Sowinski7-Nov-01 8:00 Tomasz Sowinski 7-Nov-01 8:00
 Re: What next? Nemanja Trifunovic7-Nov-01 8:22 Nemanja Trifunovic 7-Nov-01 8:22
 Re: What next? [modified] Fazlul Kabir7-Nov-01 8:31 Fazlul Kabir 7-Nov-01 8:31
 Re: What next? Tomasz Sowinski7-Nov-01 8:38 Tomasz Sowinski 7-Nov-01 8:38
 Re: What next? Anonymous20-Oct-02 10:17 Anonymous 20-Oct-02 10:17
 Re: What next? Anonymous20-Apr-05 6:13 Anonymous 20-Apr-05 6:13
 Of Power Switches and Social Ineptitude Software_Architect9-May-05 19:23 Software_Architect 9-May-05 19:23
 Re: What next? Anonymous25-Mar-02 21:13 Anonymous 25-Mar-02 21:13
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Apr-17 10:24 Refresh 12 Next »
user
The text passage provided is a technical article that explains bitwise operations in C#. It covers binary numbers, AND, OR, and shift operations, and includes example programs demonstrating these concepts.

TYPE: NO_QUESTION

The passage does not contain any explicit mathematical questions or problems to solve. It is primarily an explanatory article on bitwise operations in C#. While it does include some examples with numerical values, these are used to illustrate the concepts rather than posing questions for the reader to solve.