Let’s say, I have some user input (as a String) like “11010011011”.
Now I want to check if a bit at a bit at a particular position is set (each digit should act as a flag).
Note: I am receiving the user’s input as a String.
How can I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could work with the string as is – say you want to check the first bit on the left:
Alternatively if you want to work with a BitSet you can initialise it in a loop:
Then to check if the i-th bit is set: