Java – iterates the bits of char
•
Java
Suppose I have a char "C" whose ASCII code is 0110 0111 How do I iterate its bits? I want to build a vector from these 1 and 0@ H_ 419_ 2@
resolvent
Solution
You can easily iterate over them using bitwise operators:
char c = 'C'; for (int i = 0; i < 8; ++i) { // extract the i-th bit int b = ((c & 1<<i) >> i); // b will be 1 if i-th bit is set,0 otherwise // do whatever you want with b }
You can optimize it (as suggested in the comments):
int b = ((c >> i) & 1);
The above is the whole content of Java iterative char bits collected and sorted by programming house for you. I hope this article can help you solve the program development problems encountered by Java iterative char bits.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码