0

I am teaching myself c with some text books i've purchased. Generally this has been pretty easy but one such area i'm not grasping so easily pertains to bit operations. I understand the utilization of the operators in a literal (isolated) sense, but not so much how they apply in the specific context below.

There are 2 such examples below where I havent been able to grasp how/what they are doing in the context of some decryption algorithms.

All im looking for is a bit of an explaination of what the hexadecimal and bit shift/left right would be doing together.

Most of the examples ive been looking through online are in an isolated sense and so far arent quite sinking in to how they are being applied in this scenario.

If there are textbooks specific to bit manipulation / operations that anyone could recommend I'd also be willing to invest in such information. Such is my determination to really nail this down ( I hate moving on without fully grasping something ).

            unsigned long lBits = 0;
            lBits = ((unsigned long)(key[28] & 0xFFFF)<< 24)|
            ((unsigned long)(key[29] & 0xFFFF)<< 16)|
            ((unsigned long)(key[30] & 0xFFFF)<< 8)|
            ((unsigned long)(key[31] & 0xFFFF));

  ...

  for(i=0; i < max_length ; i++){
     unsigned long tBits = lBits | i;    
     tkey[28] = (unsigned char) (tBits >> 24);
     tkey[29] = (unsigned char) (tBits >> 16);
     tkey[30] = (unsigned char) (tBits >> 8);
     tkey[31] = (unsigned char) (tBits);
5
  • You haven't provided the declaration of key and tkey.
    – owacoder
    Commented Jul 3, 2016 at 23:05
  • yeah, i was looking to cut down the amt of code i was posting as the example is quite long. key is a char array arg passed in through the command line that is a portion of a 32 bit key, tkey is the same key padded with 0s to meet the 32bit length. Commented Jul 3, 2016 at 23:08
  • Is it an unsigned char array, or just char?
    – owacoder
    Commented Jul 3, 2016 at 23:10
  • Possible duplicate of What are bitwise shift (bit-shift) operators and how do they work?
    – buld0zzr
    Commented Jul 3, 2016 at 23:13
  • unsigned char key, tkey; Commented Jul 3, 2016 at 23:19

1 Answer 1

1

Let's break this down line-by-line:

        unsigned long lBits = 0;
        lBits = ((unsigned long)(key[28] & 0xFFFF)<< 24)|
        ((unsigned long)(key[29] & 0xFFFF)<< 16)|
        ((unsigned long)(key[30] & 0xFFFF)<< 8)|
        ((unsigned long)(key[31] & 0xFFFF));

This is pretty confusing at first. At first glance, it seems that this tries to read the big-endian 32-bit integer starting at key[28]. However, as the mask is 0xFFFF (16 bits), rather than the expected 0xFF for a typical 8 bit mask, this is not the actual effect. What is actually happening is that the bytes of key are actually being OR'd together into a 32-bit long. Given the lack of context, it's difficult to say what this is supposed to achieve, exactly. I suppose it might be mixing the bits of the key together to form a "round key" or the like.

for(i=0; i < max_length ; i++){
   unsigned long tBits = lBits | i;    
   tkey[28] = (unsigned char) (tBits >> 24);
   tkey[29] = (unsigned char) (tBits >> 16);
   tkey[30] = (unsigned char) (tBits >> 8);
   tkey[31] = (unsigned char) (tBits);

This is a bit clearer. It is simply writing tBits | i to tkey[28] in big-endian format.

In effect, this is looping over all combinations of bits in i and writing them to the 32 bits starting at key[28]. I presume that the rest of the loop body does something with these bits.

2
  • Thanks for your input.The counter iterates over the number of possible combinations to fill the partial key supplied. I dont really understand how that applies to the above though. Commented Jul 3, 2016 at 23:28
  • argh. sorry. just realised counter should be i. i copied incorrectly. :( Commented Jul 3, 2016 at 23:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.