Checksum Shift operator

I am trying to write this pseudocode
checksum = 0
for each character, C, in the word {
     rotate the (16 bit) checksum 1 bit to the right
     XOR the character into the most significant byte of the checksum
The code that I have written so far, which isnt really working is;
int csum=0;
for(int i=0;i<c.length;i++)
     csum = csum>>1;
     csum=c[i]^(csum>>8);
}c is an int array holding the word I want to generate the checksum from.
Can anyone see what is wrong? TIA

wow, thanks for the quick responses!
Is your assignment to do this directly in your code, or are you amenable to using some methods of java.lang.Integer?I can do this using which ever method I want, it is for cracking an encrypted file.The encrypted file stores a checksum in 2 bytes (in known positions) , once my program has generated a possible password it is then checked with the checksum included in the encrypted file.
How do I rotate? Is there an operator to do this?
Thanks again everyone

Similar Messages

  • Left-shift operator used on byte values

    Hello,
    I'm reviewing some problems, and I need some help. I have a program that has some code like the following:
    byte y = 10; // 00001010 in binary
    byte result = (byte) (y << 1);
    System.out.println("result: " + result); // 20.  Ok.
    result = (byte) (y << 7);
    System.out.println("result: " + result); // 0.  Ok.
    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.
    // 8 % 8 = 0 number of bits for the shift.
    result = (byte) (y << 6);
    System.out.println("result: " + result); // -128.  Ok.
    result = (byte) (y << 10);
    System.out.println("result: " + result); // 0.  Why???
    // Shouldn't it be 2 bits for the shift?
    // That is, 10 % 8 = 2.
    // I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.the result of (y << 8) is an int, not a byte. the byte "y" is promoted to int for the bit shift. so the int result of the bit shift is 00000000 00000000 00001010 00000000. when you cast that back to byte, the 24 leftmost bits get lopped off, and you're left with zero.
    hth,
    p

  • Logical right shift ( ) operator

    Hi,
    I suspect a bug in the logical right shift operator >>> (as opposed to the arithmetic right shift >>).
    The documentation says "0-s will be shifted from the left". It is not the case.
    byte b = -128;                         // this means 0x80 in hex notation
    byte b1 = (byte)(b >>> 4);I would expect b1 = 0x8 (0-s shifted in from the left) but the result is
    b1 = 0xF8 instead, as if I had used the >> operator. The sign bit was shifted in from the left instead of 0-s.
    Of course, there is a workaround:
    byte b1 = (byte)((b >>> 4) & 0x0F); which is the same operation as using >>
    If this is the case, what is the purpose of using >>> instead of >> ?
    Anyway, the ONLY thing that frustrates me in the Java language that it does not support unsigned integers. It would be important for embedded systems where unsigned numbers are used more frequently.
    Any suggestions?
    Thank you,
    Frank

    sabre150:
    It depends on the size of processor you use. The Java program runs on a 32- or 64-bit processor, so there is no problem promoting from byte to short or int. But I am talking to an 8-bit processor over a network. So I get a byte stream that has to be converted to numbers here on the Java side. The data types are various: byte or short, even sometimes two 4-bit nibbles (here comes the >>> operator), signed or unsigned. If I get a two-byte sequence, I have to convert it to a short, signed or unsigned. That means in Java promoting the byte to int, shift left the high byte, OR the low byte then cast it back to short if signed, or mask with 0xFFFF if unsigned. The resulting int can be converted to string in the usual way. With distinguished signed and unsigned types, this would be done by the compiler. But I come from the Assembler (and then Pascal/Delphi) world, so it is not a big problem for me.
    I agree that this feature is used very rarely, I already wrote the conversion routines, so why bother? As a newcomer to Java (after struggling with C for years, I love it), I just wanted to see whether other people have a better and more efficient idea.
    Thank you all for your replies!
    Frank

  • The Shift operator Question

    Dear sir/madam
    I having question for the shift operator.
    What different between >>> and >> ?
    I have refer to sun tutorial, it state >>> is unsign, what does it mean?
    and last is
    if integer is 13
    what anwer for 13>>8
    is it move for 8 right by position? pls told me in detail
    is it answer same with the 13>>>8 ?
    Thanks for ur solutions

    You can play with the following for a while.
    final public class BinaryViewer{
      final static char[] coeffs = new char[]{(char)0x30,(char)0x31};
    public static String toBinary(int n) {
      char[] binary = new char[32];
      for(int j=0;j<binary.length;j++) binary[j]=(char)0x30;
      int charPointer = binary.length -1;
      if(n==0) return "0";
      while (n!=0){
          binary[charPointer--] = coeffs[n&1];
          n >>>= 1;
    return new String(binary);
    public static void print(int n){
        System.out.print(BinaryViewer.toBinary(n));
        System.out.print("   ");
        System.out.println(String.valueOf(n));
    public static void main(String[] args) throws Exception{
       int n=Integer.MIN_VALUE;//Integer.parseInt(args[0]);
       int m=Integer.MAX_VALUE;//Integer.parseInt(args[1]);
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
        n=1024;
        m=-1024;
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
       n=2048;
       m=-2048;
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
    }

  • Shift Operation in Prodcution order.

    How to record the shift operation in the Production order?

    Hi,
    Please try to simply your subject of posting. It is not necessary your subject and body of discussion should be same.
    Yes possible to remove under SQL management studio provided you have authorization to access.
    Thanks & Regards,
    Nagarajan

  • Pda reproducible bug with logical shift operation when y 3 for arrays

    Folks,
    I have discovered a problem and wonder if this is a wider bug or if I am doing something wrong:
    Labview PDA 8.0.1  -  running on a Dell Axim:
    I create a 2x1000 array of U32 and feed it as x into the logical shift operator.  If  y is between -3 and 3, the logical shift works properly.  If y >= 4 or <= -4, then the array is returned with all zeros.
    I have reproduced this for I32 data and smaller arrays.  Logical shift works properly for scalars.
    Has anyone else seen this?  Is this a bug?
    Thanks for any help.

    It might very well be a bug. If it works differently on the PC and on the PDA then it is almost certainly one.
    I suggest you post a simple example showing this so that people with current versions of the PDA module (i.e. not me) can test this. Maybe it was even fixed for 8.2?
    Try to take over the world!

  • How can I do Shift operation in plsql

    hello,
    i want to know whether there is any package or operator to do the shift operation in plsql.
    like (myVar<<8 in c++).

    why don't you use
    myvar := mywar * power(2,8);

  • JAVA (SHIFT OPERATOR)

    Hi,
    I really want to know SHIFT operator in Java like >> , << , >>>
    Could anybody kindly help to explain??
    And
    byte a =-1;
    a = (byte)(a>>>2);
    why the output become -1??
    thanks.

    umm i did get them mixed up. preserved sign bit right shift is >>, pulling-zeros is >>>. so keep that in mind. (ie, 18>>2 == 10010>>2 == 11100 which is -4)
    To calculate the resulting number from >>> right shifts, check this: http://www.janeg.ca/scjp/oper/shift.html
    In response to your original question, it seems it's far more complicated than I at first thought. The -1 that you're getting is an odd (and rather interesting, imo) byproduct of modular arithmetic. what you wrote is exactly similar to this code (ie, my code mimics the castings that happen in your code):
            byte a = -1;
            int b = (int) a;
            int c = b>>>2;
            byte d = (byte) c;
            System.out.print(d);So what happens is A, which is a string of ones (ie 11111111) is casted to an int, B. In general, casting a byte to an int returns an int with exactly the same bits at the end with the sign bit repeated 24 times at the start (ie, 01101011-->(int)0000...00001101011). So B is 11111111111111...1111 (32 ones). Then we right shift B by 2 (we'd get the same answer right shifting it by 0, 1, 2, etc, all the way up to (and including) 24). Then we recast the int C to a byte. This is where the interesting thing happens: Depending how much we right shifted B to get C, C will be a certain number of zeros (call this Z) followed by (32-Z) ones. Apparently, in (mod 2^8) in two's complement all of these numbers are congruent to -1, so the cast gives negative one.
    (remember that the int 127 cast to byte is 127, while the int 128 cast to byte is -128. conversions to a lower-bit type always happen mod 2^N, where N is the number of bits in the type we're casting to.)
    BTW, thanks for this problem. i had a fun half hour figuring it out. hopefully it made some sense to you...
    (EDIT: Flounder is right--you can just view the cast from int to byte as keeping the last 8 bits. much easier to think of it that way, though my definition does give some intuitions about WHY all this works.)
    Edited by: a_weasel on Oct 13, 2008 8:00 PM

  • Bitwise shift operator...

    I know this is very basic but I've never done this...
    If I have an object that is an Integer, and I want to increase it by 1, isn't this how you do it?
    Object ob=new Integer(10);
    ob=ob>>1;it tells me that >> operator cannot be applied to object, int
    So what can it be applied to..?

    Ummmm,sadly, no... :(
    I really do lack basic computer skills, I don't know
    a lot about binary and stuff... but how would you do
    what I want to do..?I asked because it's quite a weird code. Just checking.
    OK, so:
    the binary shift operator can only be applied between to ints, that is primitives, no Objects (like Integer is).
    This operator is not used to increase a number, but to shift its bits by one to the right.
    Usage:
    int i,j;
    j = 10;
    i = j >> 1;
    Integer obj = new Integer(10);
    int res = obj.intValue() >> 1;
    Integer resObj = new Integer(res);

  • Shift operator in work center

    Our plant works in 3 shifts. for every shift there are total six persons working on it. 2 in each shift.
    we need to assign the name of person working on the particular machine. Where can be the assignment  of the actual person working on the workcenter be made.
    thanks and warm regards.
    Edited by: pankaj lade on May 8, 2008 12:32 PM

    Hi,
    One information required..
    Why you wanted to have the operator names to be attached to your work centers?
    You can have the Operator Names at the time of Confirmation of the Operation (This is what you wanted for Tracking purpose?)
    1. Have the Operator Name or No. in Personal Number Filed in CO11N (you need to actvate the HR Transactions)
    2. Activate a Stadard Value key with Operator and during confirmation the relevnt operator can enter their name or No. there
    3. in CO11N text field as the operator to enter their name..
    Hope this helps..
    Regards,
    Siva
    Edited by: Siva Kumar M on May 8, 2008 4:22 PM

  • Shift operation on Weekly off?

    Hi
    Gurus,
    what to do when i have  to run Production on Weekly Off during urgency ?
    We have weekly off on tuesday on this day i want to carried out production.
    Should i define / change holiday calender? or what......
    Plz guide me
    Harish

    Hi,
    Goto Workcenter where you want to perform operations,
    CR02, goto Capacities view, click on capacity header, click on intervals & shifts,  For valid period select in workdays column maintain 1- Work day( overides factory calander). then its a working day for work center, although its a factory declared holiday also.
    Regards
    Jagadeesh K

  • Shift operator operations

    int i=1;
    i <<= 31;
    i >>= 31;
    i >>= 1;
    can anyone explain how the result i value=-1
    thanks

    The high order bit of an integer is the sign. The ">>" operator maintains the sign. See the results of your shifts below which results in a value of -1. Use ">>>" if you do not want to propagate the sign.
    i = 1      00000000000000000000000000000001
    i <<= 31   10000000000000000000000000000000
    i>>= 31    11111111111111111111111111111111
    i>>=1      11111111111111111111111111111111

  • Can shift operator overflow?

    say i have
    int a=99;
    int b=1;
    what's going to happen if I do
    a=b<<99;

    Bit-shifting wraps... So, left-shifting a 32-bit
    number by 99 is the same thing as left-shifting it by
    99 % 32, or 3. You'll get the same result if for both
    1 << 99 and 1 << 3.No it doesn't (although the result is the same.)
    It truncates the right hand argument to a number of bits. The number of bits is defined by the type of the left hand argument.
    So when i is an int then....
    (i << 99) becomes (i << (99 % 32))
    This is constrained not only in the JLS....
    http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
    But also in the JVM spec....
    http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc6.html#ishl

  • Left Shift Operator

    class LeftShift
    public static void main(String aa[])
      int a = 1;
      for(int j=0; j<67; j++)
       int k = a << j;
       System.out.println("Shifting by " + j + " : "  + k);
    }This code gives the following outout
    Shifting by 0 : 1
    Shifting by 1 : 2
    Shifting by 2 : 4
    Shifting by 3 : 8
    Shifting by 4 : 16
    Shifting by 5 : 32
    Shifting by 6 : 64
    Shifting by 7 : 128
    Shifting by 8 : 256
    Shifting by 9 : 512
    Shifting by 10 : 1024
    Shifting by 11 : 2048
    Shifting by 12 : 4096
    Shifting by 13 : 8192
    Shifting by 14 : 16384
    Shifting by 15 : 32768
    Shifting by 16 : 65536
    Shifting by 17 : 131072
    Shifting by 18 : 262144
    Shifting by 19 : 524288
    Shifting by 20 : 1048576
    Shifting by 21 : 2097152
    Shifting by 22 : 4194304
    Shifting by 23 : 8388608
    Shifting by 24 : 16777216
    Shifting by 25 : 33554432
    Shifting by 26 : 67108864
    Shifting by 27 : 134217728
    Shifting by 28 : 268435456
    Shifting by 29 : 536870912
    Shifting by 30 : 1073741824
    Shifting by 31 : -2147483648
    Shifting by 32 : 1
    Shifting by 33 : 2
    Shifting by 34 : 4
    Shifting by 35 : 8
    Shifting by 36 : 16
    Shifting by 37 : 32
    Shifting by 38 : 64
    Shifting by 39 : 128
    Shifting by 40 : 256
    Shifting by 41 : 512
    Shifting by 42 : 1024
    Shifting by 43 : 2048
    Shifting by 44 : 4096
    Shifting by 45 : 8192
    Shifting by 46 : 16384
    Shifting by 47 : 32768
    Shifting by 48 : 65536
    Shifting by 49 : 131072
    Shifting by 50 : 262144
    Shifting by 51 : 524288
    Shifting by 52 : 1048576
    Shifting by 53 : 2097152
    Shifting by 54 : 4194304
    Shifting by 55 : 8388608
    Shifting by 56 : 16777216
    Shifting by 57 : 33554432
    Shifting by 58 : 67108864
    Shifting by 59 : 134217728
    Shifting by 60 : 268435456
    Shifting by 61 : 536870912
    Shifting by 62 : 1073741824
    Shifting by 63 : -2147483648
    Shifting by 64 : 1
    Shifting by 65 : 2
    Shifting by 66 : 4
    Things are fine till shifting by 31, but I don't understand why the entire pattern repeats after that.

    Well, int has 32 bits, and when you start left shifting 1, which has one at first position and zero at all other, you keep shifting this one to left, and keep inserting zero from the right side. But when you shift it by 32, the only available one should gets off the available 32 positions and I expect to have zero at all the positions, that should give me output as 0. It, however, is giving 2!
    a = 1 00000000 00000000 00000000 00000001
    a<<1 00000000 00000000 00000000 00000010
    a<<2 00000000 00000000 00000000 00000100
    a<<31 10000000 00000000 00000000 00000000
    a<<32 00000000 00000000 00000000 00000000and therefore, I expect to get 0

  • Shift Operator

    what are shift operators? and how these are implemented?

    java_tracker wrote:
    how do i know that a number is in ordinary binary format or inTwo's complement? Is it a good practice to employ Two's complement method only while dealing with Binary Numbers.All signed types in Java use two's complement. The only alternative is one's complement (in which the highest order bit is simply a sign-bit), but it's used almost never.
    You usually need not care about this in any way, since Java handles this stuff for you. The only time, when you have to know about it, is if you need to do some bit-manipulation (which should be rather rare in the field where Java is used).

Maybe you are looking for