0

I have a integer array, int KEY[32], that stores the binary representation of a four letter quadgram, e.g.:

char quadgram[4] = {'T','I','O','N'};

Where the binary representations of the characters are:T => 01010100, I => 01001001, O => 01001111, N => 01001110. I have converted these binary numbers into the integer array:

KEY[32] = 01010100010010010100111101001110;

Now I need to convert this KEY into its literal binary value, i,e:

int KEY_BIN = 1414090574; // in decimal.

How would I accomplish converting KEY into KEY_BIN?

1
  • You need to use bit shifting operators (<< or >>) and the binary OR operator |. Commented Oct 4, 2021 at 23:23

2 Answers 2

1

Given the array of integers, you can loop over them, bitshift the number by the appropriate numbers of bits, and then bitwise OR it with your result value. You'll want KEY to be an array of unsigned ints in order to have well-defined bitshift behaviour.

A working example:

#include <stdio.h>

int main() {
   unsigned int KEY[32] = {0, 1, 0, 1, 0, 1, 0, 0, 0, 
                           1, 0, 0, 1, 0, 0, 1, 0, 1, 
                           0, 0, 1, 1, 1, 1, 0, 1, 0, 
                           0, 1, 1, 1, 0 };
   unsigned int result = 0;
   for ( unsigned int i = 0; i < 32; ++i ) {
      unsigned int shiftAmount = 32 - i - 1;
      unsigned int shifted = KEY[i] << shiftAmount;
      result |= shifted;
   }

   printf( "%u\n", result ); // Prints 1414090574
}
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! That works perfectly. I just changed auto to int. I have a lot to learn; this bitshifting confuses me. Thank you!!
The middle step KEY array is completely superfluous and makes the code inefficient.
(And yeah using C++11 auto in bitwise arithmetic will blow your whole leg off. Don't use C++(11) in hardware related programming.)
0

I have converted these binary numbers into the integer array

Why? That's not a helpful format for the computer, it's just a superfluous middle step. Get rid of that, it isn't needed.


How to store values in an integer depends on endianess, What is CPU endianness? If we don't understand this first, then no can do.

Now what you call "literal binary value" is actually the big endian representation, with the first character stored at the most significant address. We can make endianess portable code by using bit shifts to always shift the first letter to the ms byte:

#include <stdio.h>
#include <stdint.h>

int main (void)
{
  char quadgram[4] = {'T','I','O','N'};
  uint32_t val;

  val = (uint32_t)quadgram[0] << 24 |
        (uint32_t)quadgram[1] << 16 |
        (uint32_t)quadgram[2] <<  8 |
        (uint32_t)quadgram[3] <<  0 ;

  printf("%d\n", val);
}

2 Comments

So in your example val will equal 1414090574? So I think what I am doing is dumb to begin with. I am writing a cryptoanalysis routine to decrypting a substitution cipher, by implementing a "hill-climbing" algorithm that uses the statistical analysis of English quadgram probabilities to determine the fitness of my various attempts at cracking the key (if that makes any sense). The points is, I have a list of over two million quadgrams and their associated probability (how often each quadgram is used in the book War and Piece...which is just an arbitrary text). ...
I need to store all of these two million quadgrams in some kind of look-up table. I am using a hash-table. I am using the four characters of each quadgram as the key, so that I can look them up when i find a matching quadgram in the encrypted text. My porblem was that there were hundreds of repeated keys when I only took as the key the sum of the quadgram's ascii values, e.g., TION and NOIT and ONIT all would have the same value. So I figured, the binary representation of these quadgrams would all be unique and that would make my hash function very simple. So yeah, I'm confused. Thanks also!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.