C# Bitwise Encoding

C# Bitwise Encoding

Hi everyone,

In this post I will talk about C# Bitwise Encoding.

As I told you in my Simple Encoding with C# article we are now continuing to develop our Encryption application. If you still haven’t read it, please review it before going on with this article.

Just to make our application appropriate for OOP principles, I am going to alter my application which I coded in the previous article a bit. To do so..

  • Right click on your project name which was CryptographyApp and follow Add > Class path. I am gonna add an interface named IEncryption.cs in which I am gonna hold the methods to Encode and Decode. The interface should look like that:
  • I made my interface public so that I will be able to use its reference in my Form(Form1).

string Encode(string str); method takes a string parameter which represents the string to Encode and our method returns string to get the encoded text as a string.
string Decode(string str); method takes a string parameter which represents the string to Decode and our method returns string to get the decoded text as a string.

  • The next step we are going to do is adding two new classes to our project that implements our IEncryption interface.  We do it the same way how we add our interface above. Just name the first class as SimpleEncoder which is going to include the encryption codes that we have already written, and name the second class as BitEncoder which is the main purpose of this article.
  • Implement your SimpleEncoder and BitEncoder classes from IEncryption interface writing : IEncryption at the end of their definition line and Left Click on IEncryption word to see the hint at the beginning of the word “IEncryption“, If you have come to this level, do as in the picture below to implement aour methods(I showed it just for SimpleEncoder class, you must do the same for BitEncoder class as well)
  • Remove throw lines from your methods. Also copy what you already have in your btnEncode_Click method of your form and paste it into your Encode method in your SimpleEncoder class.

This SimpleEncoder class is to do the job what we have done in the previous article. So not much thing is going to change.

  • Just change rtbEncryption.Text with str which we will get from our parameter value not RichTextBox so far. And instead of rtbEncryption.Text = cryptText; in the end, write return cryptText;
  • Do the same thing for your btnDecode_Click method content with your Decode method in your SimpleEncoder class.

Now make sure that you have a SimpleEncoder class that looks like as in the picture below.


We are going to change the appearance of our form a little bit. To do so..

  • Add two Labels, two RadioButtons, and make sure that their properties are like:

First Label: Text – “Encryption Method” :  , Name doesn’t matter

Second Label: Text – “Enter text to ENCODE :” , Name – lblTitle

First RadioButton: Text – “Simple”, Name – rdbSimple

Second RadioButton: Text – “Bit”, Name – rdbBit, Checked – True(to make it default choice)

  • And have your form appear like :

Until now; We just made our application a bit appropriate for OOP principles and make it ready for writing Bit Encoding codes.

Now to let you have an opinion about Bit encoding I am going to explain it.

Remember our Simple encoder, it gets the next ascii table value of the actual char. The thing we are gonna do with bit encoding is far different and more approprite to have an Encryption application. We will define a variable that has the type of ushort to process our chars to encode or decode with bitwise encryption.

^ operator in C# does this job. It takes two arguments and process the EXOR operation. In other words it computes the bitwise exclusive-OR of its operands. That is, the result is true if and only if exactly one of its operands is true.

Logical Definition of ^opearator:

result of true ^ true is false

result of false ^ false is false

result of true ^ false is true

result of false ^ true is true

In bitwise logic the trues and falses are considered as 1‘s and 0‘s. 1 represents true and 0 represents false.

Our chars to be encoded or decoded are represented as bits as you already know.

To give an example let’s process char ‘a’ and ‘1’ and see the result.

In ascii table letter ‘a’ has the value of 97 and it makes 0011100100110111 in bit format. And in ascii table number ‘1’ has the value of 49 and it makes 0011010000111001 in bits.

If we write ‘a’ ^ ‘1’ into our program in Visual Studio it will process their represented values in bits and do 0011100100110111 ^ 0011010000111001  process. It will return 80 value in ascii table to us. It represents ‘P‘ letter in ascii table. That’s the logic of our Bit Encryption method.

  • Now, let’s define our key to do EXOR process with our chars and define its value when construction of BitEncoder.

  • To write our Encode and Decode method you don’t need to do so much things. Just copy your SimpleEncoder’s Encode method content and paste it into BitEncoder’s Encode method. Just don’t forget to change (char)(str[i] + 1) with (char)(str[i] ^ key) . Do the same for Decode method with Decode method in SimpleEncoder class and That’s it!
  • Now we are done with BitEncoder class. Don’t forget to save your changings 🙂 Please make sure if your BitEncoder class looks like:

Now, it’s time to prepare our presantation layer 🙂 I mean the Form1 form part and code behind..

  • Define a IEncryption reference, so that we can access both SimpleEncoder and BitEncoder objects with the same reference. And also define two string variables to represent our plain text and coded text. Have them like:

  • Write two methods named Encode and Decode that has two parameters (string str, IEncryption e) and also have them like:

Line 26: I am assigning the reference of IEncryption that comes from my parameter(Encode) to my reference of IEncryption that I defined in class(Form1)

Line28: I am assigning the string in RichTextbox to my plain varibale.

Line29: Processing Encode method of the class that IEncryption represents(either SimpleEncoder or BitEncoder) and assigning its value to my coded variable.

Line30: Assigning this coded value to my Text property of RichtextBox.

  • For Decode method I do something very similar.
  • Inside of btnEncode_Click and btnDecode_Click methods I am gonna control whether rdbSimple or rdbBit is checked and according to their Checked values I am gonna call my Encode and Decode  methods which I have written in Form1 class. And also alter the Text property or lblTitle to make our application more visual to the users 🙂

What we have in our Click events are..

Note: I sent 27 by paramater to BitEncoder’s constructor method, you can send any value to get different result from your method.

  • The last thing we’re gonna do is adding a line into btnClear_Click method.

Now we have done with our Encryption Application.

Here is a Screenshot of the application. In my example I worked with “mehmetgunen.com” text and get this result.

You can either get encoded form of your strings encoded by SimpleEncoder or BitEncoder class. Hope you liked my article. Thank you!



  • Thank you Furkan for the reminder. Just to make it clear to my followers I should say that: BitEncoder class has a key to process encryption so it is an Encryption and SimpleEncoder class has no key to process encryption, it has only a line of algorithm to do encoding, so it is an Encoding process.

Leave a Reply

Your email address will not be published. Required fields are marked *