C++Builder Learn Encoding And Decoding Data Files By Using Bit Shifting

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn Encoding And Decoding Data Files By Using Bit Shifting
By Yilmaz Yoru August 20, 2021

What is the fastest data encoding and decoding method ? How you can secure your data by a fastest method ? Can we use shifting to encode or decode a data ? Can we use shifting on Strings or on Binaries data? When you read and test examples below, you will have answers to all these questions.

The Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся post before. Now let’s see Bit Shifting and Encoding – Decoding examples.

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,
C++:
c = c << 1; // Encoding with Left Bit Shifting
c = c >> 1; // Decoding with Right Bit Shifting
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,
C++:
c =(c << 2)|(c >> (8 - 2); // Encoding with Circular Left 2 Bits Shifting
c =(c >> 2)|(c << (8 - 2); // Decoding with Circular Right 2 Bits Shifting

Circular Left Shifting and Circular Right Shifting with Complexity​

We can add complexity to number of shifting by adding (1+i%7) for example,
C++:
c =(c << (1+i%7))|(c >> (8 - (1+i%7)); // Encoding by Circular Left Bits Shifting with Complexity
c =(c >> (1+i%7))|(c << (8 - (1+i%7)); // Decoding by Circular Right Bits Shifting with Complexity

Bit Shifting Data Files​

For example we can use this shifting method to encode and decode data files as in this example below,
C++:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
 
int main()
{
   unsigned char c;   // std::bitset<8> x;
   unsigned int i=0;
 
   std::ifstream indata;
   std::ofstream outdata;
 
   // ENCODING mytest.txt to mytest.data
   // ---------------------------------------------------------
   indata.open("D:\\mytest.txt", std::fstream::binary);
   if(!indata)
   {
   std::cerr << "Error: Input file could not be opened\n";;
   exit(1);
   }
 
   outdata.open("D:\\mytest.dat", std::fstream::binary);
   if(!outdata)
   {
   std::cerr << "Error: Output file could not be opened\n";;
   exit(1);
   }
 
   std::cout << "\nEncoding..\n";
 
   i=0;
   while ( !indata.eof() )
   {
   c=indata.get();  // get the next char of data  or indata.get();
 
   // c = c << 1; // Encoding with Left Bit Shifting
   c =(c << (1+i%7))|(c >> (8 - (1+i%7))); // Encoding by Circular Left Bit Shifting with Complexity
   outdata.put(c); // out the encoded char
   std::cout << c;
   i++;
   }
 
   indata.close();
   outdata.close();
 
   // DECODING mytest.dat to mytest2.txt
   // ---------------------------------------------------------
   indata.open("D:\\mytest.dat", std::fstream::binary);
   if(!indata)
   {
   std::cerr << "Error: Input file could not be opened\n";;
   exit(1);
   }
 
   outdata.open("D:\\mytest2.txt", std::fstream::binary);
   if(!outdata)
   {
   std::cerr << "Error: Output file could not be opened\n";;
   exit(1);
   }
 
 
   i=0;
   std::cout << "\nDecoding..\n";
   while ( !indata.eof() )
   {
   c= indata.get();  // get the next char of data
   //c = c >> 1; // Decoding with Right Bit Shifting
   c =(c >> (1+i%7))|(c << (8 - (1+i%7))); // Decoding by Circular Right Bit Shifting with Complexity
   outdata.put(c); // out the encoded char
   std::cout << c;
   i++;
   }
 
   indata.close();
   outdata.close();
 
   getchar();
   return 0;
}
We can use this method to protect our data, user names, passwords etc. 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 passwords and user names etc. See this Для просмотра ссылки Войди или Зарегистрируйся