C++C++11C++14C++17Introduction to C++Learn C++

What You Need For Encoding Strings Using Bit Shifting

What You Need For Encoding Strings Using Bit Shifting

What is the fastest string coding and decoding method? What is the fastest method to secure the contents of your string? Can we use shifting to encode or decode a string? Can we use shifting on Strings or on Binary data? Do strings help me build C++ applications with the use of a C++ compiler? Let’s answer these questions.

If you are working with Wide Strings, then you should read this additional article that specifically discusses wide strings.

What are bitwise operations?

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 some Bit Shifting and Encoding – Decoding examples.

How can I simply encode and decode a String in C++?

As in this bitset, we can do this operation on integers, floating numbers, chars, strings or binary data. Because all data operations are in done bit form in real. We can use bitwise left shifting operation to encode a data or we can use right shifting operation to decode this encoded data.

We can simply left shift (or right shift) the character of a string and we can simply right shift (or left shif) the character of a string as below,

This example below encodes and decodes string.

Output should be somethings like this, encoded characters may be different because of font will be used.

How can we encrypt a String with more complex encoding?

If you check this example output above each character symbol refers to another character symbol. If we want to avoid this we can add a complexity function which is iterated by the index number of the String Member. We can add complexity function to bitwise operation for each index of elements. For example we can use (index%8). So these kind of data is hard to encode if you don’t know this complexity function. You can formulate your own function. Be sure it is reversible and depends on index number, and some other parameters. We can use Circular Bit Shifting with Complexity to encode or decode wide strings as 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)

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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++