1

I have a number like 100100010000001010001 and when I store a number in some variable like:

let numb =  100100010000001010001

it gives me the error:

Integer literal '100100010000001010001' overflows when stored into 'Int'

I have also tried the other ways like:

var number:UInt64 =  100100010000001010001

I just want to add another number to this number. Can anyone tell me how I can do this? I also tried:

NSDecimalNumber(value: 100100010000001010001)

and it does not work either.

2

1 Answer 1

2

Swift allows you to write integer literals in different bases:

  • Binary: 0b100101
  • Octal: 0o47
  • Hexadecimal: 0xd4f
  • Decimal: 3456 (no prefix)

This is covered in the Numeric Literals section of the Swift book.

So your binary number would be written as:

let numb = 0b100100010000001010001

Or you can create the Int from a string using a radix:

let numb = Int("100100010000001010001", radix: 2)
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.