C++C++11C++14C++17Generic ProgrammingIntroduction to C++Learn C++

Learn Encoding And Decoding Data Files By Using Bit Shifting

Learn Encoding And Decoding Data Files By Using Bit Shifting

The bit is the most basic unit of information in computing and digital communications. In real all operators are mainly based on Bit Operations which are also called Bitwise Operations. In computer programming, a Bitwise Operation operates on a bit string, a bit array, or a binary numeral (considered as a bit string) at the level of its individual bits, 1s, and 0s. The Bitwise Operation is basic to the higher-level arithmetic operations and it is a fast and simple action because it is directly supported by the processors. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

Because of all these basics of the microarchitecture of computers, it is very important to know Bitwise Operators. C Programming language is one of the oldest programming languages and a lot of operands, operators in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages. We have explained well about operators in general in this Learn How To Use Operators In C++ post before. Now let’s see Bit Shifting and Encoding – Decoding examples.

In this post, you’ll learn about the fastest data encoding and decoding methods; how to secure data using the fastest method; how to encode or decode data using shifting, and how to use shifting on Strings or Binaries data. By learning more about bit shifting, it will help you to easily build C++ applications using the C++ IDE.

Bit Shifting Data

One of the Bitwise Operand is the Bit Shifting, the Left Shifting with ‘<<‘ operand, and the Right Shifting>>‘ operand. Bit operations are the fastest operations in machine codes and in C++ because of the microarchitecture of computers as explained above. There are many encoding and decoding methods, also hash coding methods. One of the easiest and the fastest encoding method is Bit Shifting Data. We can use this method to encode and decode data files.

Left Shifting and Right Shifting

For example if c is char we can encode and decode this char as below,

This works well in lower than 127 char numbers. When shifting we lost the frontier bits (when shifting left we lost left bit or bits and when shifting right we lost right bit or bits). To hold all these bits in a binary data we should do Circular Bit Shifting,

Circular Left Shifting and Circular Right Shifting

If you we use Circular Bit Shifting, we never loose any bits when we encode or decode our data. If we want to shift 2 bits from maximum of 8 bits we can do left and right circular bit shifting as below,

Circular Left Shifting and Circular Right Shifting with Complexity

We can add complexity to number of shifting by adding (1+i%7) for example,

Bit Shifting Data Files

For example we can use this shifting method to encode and decode data files as in this example below,

Bit shifting is the fastest data manipulation. We could use this bit shifting method to protect our data, user names, etc. and similar sensitive items as a fun exercise but bit shifting in this way is a form of security through obscurity. We can use this with the support of a second industrial security system There are industry standard security practices such as salting passwords and using industry standard encryption that provide real security.

We can also hash this Encoded String by using powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins, etc.). So you can put another dimension to your data systems. See this Learn To Use Powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins)


RAD Studio is a powerful and modern IDE for C++. Why not download a trial copy today and see what it can do for you?

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

C++C++14C++17C++20Learn C++

What Are The C++14 Features Removed From C++17?

C++C++11C++14C++17C++20Learn C++Syntax

What is the conjunction (std::conjunction) metafunction in C++?