1

Is there any solution to find a specific binary bit value inside a string representation of a binary number? For example, I have a binary string with 64 bit length:

0000000000000000000000000001000000000000000000100001001000000000

I need to find and return the value in the 28th bit, starting from the left. In the example above, it's equal to 1.

4
  • Q: Homework? Q: What could be easier than 1) an array, 2) the binary value of one digit at a time? Or, if you only want one bit ... then access the array offset directly? Commented Aug 9, 2012 at 21:37
  • This is @"Control Panel\Desktop\UserPreferencesMask" registry key binary value ! And i need found existing values. Commented Aug 9, 2012 at 21:58
  • with examples from other guys , i found other values from binary string ! Commented Aug 9, 2012 at 21:59
  • @paulsm4 i write small winos tweaker ! Commented Aug 9, 2012 at 22:00

4 Answers 4

3
int val = s[28 - 1] - '0';  ///////
Sign up to request clarification or add additional context in comments.

2 Comments

Simpliest way to solve my question ! And works great ! Thanks alot !
Interesting way, didn't realize subtracting characters actually subtracts their Unicode values. Of course, let's hope the binary string isn't composed of the boldish numbers toward the end of unicode table: 528491
1

Might be a language barrier here, but this sounds like a good place for indexOf? If you need to take a specific portion of a string I might do this:

//string array for example
foreach(string BinaryString in BinaryStrings[])
{
  string TwentyEightBitSub = BinaryString.SubString(28);
  if(TwentyEightBitSub.indexOf("00000010") != -1)
  {
    //do something with matched string 
  }

}

Comments

1

Well, the value on 28th bit from the left is the 2^36 for the value of 64 bit (64-28).

So just make a binary & with your value and if it's changed, means that on 28th from the left was a 0, if not was a 1

long value = ... 
//binary rapresentation value is (say) 

0000000000000000000000000001000000000000000000100001001000000000

so , if

if(value & 2^36 != value) //binary AND 
    return 0; //28th was 0
else
    return 1; //28th was 1

If this is not what you're asking for, please clarify.

2 Comments

@user1562839: I'm afraid here is not much choice then using simple binary operations
@user1562839, the point made by this answer is that you should not use strings to represent binary data. It's inappropriate, not to mention massively inefficient. Once that's been established, the only way to work with numbers in binary is via simple bit operations!
0

If you always know that the string will be 64 bits (leading zeros never absent), then this will work:

private int getBit(string binString, int index) {
    if (binString == null)
        throw new ArgumentNullException("binString");
    if (binString.Length != 64)
        throw new ArgumentException("binString was not of length 64.", "binString");
    if (index < 0 || index > 63)
        throw new ArgumentException("index was not in [0,63]", "index");

    char theBit = binString[binString.Length - (1 + index)];
    if (theBit == '0')
        return 0;
    else if (theBit == '1')
        return 1;
    else
        throw new ArgumentException("The supplied string was not binary.", "binString");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.