how do i store byte array of16 elements as one no.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jatinch
    New Member
    • Mar 2010
    • 7

    how do i store byte array of16 elements as one no.

    i have a byte array having 16 elements i.e 128 bits.. i want to do bit manipulations on this 128 bit byte array
    how can i store this 128 bit as one no.so i can do bit manipulations
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Are you talking about an byte array have size 16, eg byte[16], or one massive number with 128 bits?

    If the latter, then you can write your own class or use a BitSet

    What bit manipulations are you talking about specifically? &, |, ~, >>, >>>, <<, HighestSetBit, LowestSetBit, # of set bits, etc...

    Comment

    • jatinch
      New Member
      • Mar 2010
      • 7

      #3
      i have a byte array say byte_array[16]
      i want to perform a circular shift on the byte array

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        You'll have to create your own class. Do you shift frequently, or only occasionally? Are you getting the elements by the byte?

        If you shift a lot, one possibility is to keep an integer representing the shift, and adjust the result bytes whenever you access it.

        Comment

        • jatinch
          New Member
          • Mar 2010
          • 7

          #5
          i am implementing idea encryption algo
          i have to perform a left circular shift by 25 on the byte array i.e byte_array[16] which is nothin but my key..
          then take some elements from the byte array to form the subkeys and then again perform circular left shift by 25 on the entire byte array to generate subkeys for the next rounds.. i know how to do a circular shift
          but the problem is byte_array[16] has altogether 128 bits.. nd i dont know where to store this 128 bits to perform a circular shift operation..

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Assuming byte_array[0] is the leftmost set of bits.
            One problem you will encounter is that java does not have unsigned bytes. Thus we are using a ushort[] array instead in this example:

            Code:
            ushort[] shiftLeft(ushort [] byte_array, int shift){
              int length = byte_array.length;
              short index;
            
              shift %= length * 16;
              short wholeByteShift = shift/16;
              short partByteShift = shift%16;
            
              ushort[] retVal = new ushort [length];
              for (int i = 0; i < length; i++){    
                retVal[i] = byte_array[(i+wholeByteShift)%length] << partByteShift;
                retVal[i] |= byte_array[(i+1+wholeByteShift)%length] >> partByteShift;
              }
              return retVal;
            }

            Comment

            Working...