S
S
supreme192018-12-08 22:35:40
Java
supreme19, 2018-12-08 22:35:40

How to make a cyclic shift of bits in a byte to the left or to the right?

I am making a program for encrypting text, you need to use bitwise shifts in a byte, without losing information. It doesn’t work, please tell me what I’m doing wrong!

package z;

import java.util.Scanner;

public class Main {

//    static byte right(byte a, byte s) {
//        s %= 8;
//        return  (byte)((a >> s) | (a << (8 - s)));
//    }

//
//    static int left(byte a, int s) {
//        return (byte)((a << s) | (a >> (8 - s)));
//    }
    private static byte rigth(byte a, byte s) {
    s %= 8;
    return  (byte)((a >> s) | (a << (8 - s)));
}
    private static byte left(byte a, byte s) {
        s %= 8;
        return  (byte)((a >> s) | (a << (8 - s)));
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in); // .useDelimiter("\\s+");
        System.out.println("enter line:");
        String line = in.next() + in.nextLine();
        System.out.println("enter key:");
        int pwd = in.nextInt();
        byte[] chars = line.getBytes();            // символы строки
        String encrKey = Integer.toBinaryString(pwd); // перекодированный ключ
        System.out.println(encrKey);
        for (int i = 0; i < chars.length; i++) {
            String keyPart = new StringBuilder().     // фрагмент ключа из смежных символов в виде строки
                    append(encrKey.charAt(i % encrKey.length())).       // i % encrKey.length() не выходит за границы массива
                    append(encrKey.charAt((i + 1) % encrKey.length())). // после перебора символов начинает с начала ключа
                    toString();
            switch (keyPart) {
                case "10":
//                    System.out.println(chars);
//                    System.out.println("left");
//                    System.out.println(chars[i]);
//                    chars[i] = (byte)(chars[i] >> 1);
//                    System.out.println(chars[i]);
                    System.out.println(chars[i]);
                    System.out.println("now right");
                    System.out.println(chars[i]);
                    chars[i] = rigth(chars[i], (byte)1);
                    System.out.println("now left");
                    chars[i] = left(chars[i], (byte)1);
//                    chars[i] = (byte)((int)Integer.rotateRight(chars[i] >> 1,1));
//                    chars[i]  = right(chars[i],1);
                    System.out.println(chars[i]);


                    break; // циклический сдвиг (обратимый)
                case "01":
//                    System.out.println("rigth");

//                    chars[i] = (char) Integer.rotateRight((int) chars[i], 1);

                    break;
                case "11":
//                    chars[i] = (char) ~chars[i];
                    break;

            }
        }
    }


}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question