简体   繁体   中英

Shifting binary bits left in Java

I am having a bit of trouble with some shifting of a string in Java. Essentially I have my Stringbuilder called finalBinary which contains the string of binary bits, "00110101" and I am trying to shift it to the left 5 times, like so;

Input: 00110101

Output: 10100110

I have been reading about bitInversion on the Oracle website and some of the forums on here, but am having no luck :( I really appreciate the help, thankyou so much guys! :)

What you are describing is rotating not shifting. Also you are not rotating bits you are manipulating a String of char s which happen to represent binary. It could be "abcdefg" => "efgabcd" as far as the computer is concerned.

public static String rotateChars(String str, int count) {
    count % = str.length();
    return str.substring(count) + string.substring(0, count);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM