0
"11001001101000000100010101"

"00111000000011010000101011"

AND

= 00001000000000000000000001

Binary representation:

"11001001101000000100010101" = > 0b11001001101000000100010101

"00111000000011010000101011" = > 0b00111000000011010000101011

Two strings but value looks like binary and I want to Bitwise And Operator of two binary.

How to convert string to binary?

3
  • So you want to bitwise AND two binary strings? Do you want the result to be an Int or a String? Commented May 24, 2020 at 9:18
  • want the result is Int. Commented May 24, 2020 at 10:06
  • Then the res variable in Joakim Danielson's solution is what you need. Commented May 24, 2020 at 10:08

1 Answer 1

1

Both UInt and String has init methods where you can give the radix (base ) of the number

let s1 = "11001001101000000100010101"
let s2 = "00111000000011010000101011"

if let binary1 = UInt(s1, radix: 2), let binary2 = UInt(s2, radix: 2) {
    let res = binary1 & binary2
    print(String(res, radix: 2))
}

output

1000000000000000000001

Sign up to request clarification or add additional context in comments.

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.