Left Circular Shifting a size-16 byte-array by 25

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

    Left Circular Shifting a size-16 byte-array by 25

    //the code is as follows

    package test;
    //import java.lang.Integ er;
    /**
    *
    * @author Niket
    */
    public class Main {

    public static void main(String[] args) {
    int set=0;
    byte one = 1;
    String s = new String();
    s = "ponmlkjihgfedc ba";
    byte b[] = s.getBytes();
    for(int i=0; i<b.length; i++)
    System.out.prin t((char)b[i] + " ");
    System.out.prin tln();
    for(int i=0; i<=25;i++) { //for 25 circular left shifts
    byte t[]= b; // temp array for each left shift
    for(int j=0;j<16;j++){
    if(t[j]<0) //checks if msb==1
    set=1;
    b[(j+1)%16]<<=1;
    if(set==1)
    b[(j+1)%16]|=one;//if msb of previous is one then lsb of nxt is set to one otherwise default is anyways 0
    }
    for(int k=0; k<b.length; k++)
    System.out.prin t(b[k]+" ");
    System.out.prin tln();
    }
    for(int i=0; i<b.length; i++)
    System.out.prin t(b[i] + " ");
    }
    }

    //the output is anomalous and is as follows:
    //been breaking our heads on this so PLEASE HELP!!
    /*According to our calculations the final output after the 25 iterations is
    -56 -54 -52 -50 -48 -46 -44 -42 -40 -38 -36 -34 -62 60 -58*/
    p o n m l k j i h g f e d c b a
    -31 -34 -35 -37 -39 -41 -43 -45 -47 -49 -51 -53 -55 -57 -59 -61
    -61 -67 -69 -73 -77 -81 -85 -89 -93 -97 -101 -105 -109 -113 -117 -121
    -121 123 119 111 103 95 87 79 71 63 55 47 39 31 23 15
    15 -9 -17 -33 -49 -65 -81 -97 -113 127 111 95 79 63 47 31
    31 -17 -33 -65 -97 127 95 63 31 -1 -33 -65 -97 127 95 63
    63 -33 -65 127 63 -1 -65 127 63 -1 -65 127 63 -1 -65 127
    127 -65 127 -1 127 -1 127 -1 127 -1 127 -1 127 -1 127 -1
    -1 127 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Your problem:
    b[(j+1)%16]<<=1;

    This will not affect the negative/positive bit; it will always stay the same.
    Use unsigned bytes, and test if the number is > Byte.MAX_VALUE instead.

    .. wait, Java doesn't have unsigned bytes.
    test if the (number & (1 << 6)) != 0
    and set the top bit accordingly.
    Last edited by jkmyoung; Apr 12 '10, 03:21 PM. Reason: no unsigned bytes.

    Comment

    Working...