1

Academically Natured Question:
How can the byte data of an Int32 with the value of -1 be cast into an UInt32? (can SWIFT do that?)

Understanding:
I know that -1 isn't a value that can be represented by an Unsigned Integer, as UInts only contain Integers above -1

However I also know that both Int32 and UInt32 take the same amount of space in Bytes (4*8=32).
That byte space should be able to be used for either type, regardless if it represents the same value... which it obviously wouldn't.

Conclusion:
There should be some simple way to take the raw bit data of an Int32 and use it for a UInt32...

1
  • What have you tried? Why is this a Swift/Objective-C question (unless you show code, this is a general numerical question)? Commented Dec 18, 2014 at 22:23

1 Answer 1

2

Cast the variable via bitPattern (thanks Jthora). Plenty of help here on SO for this:

For 32-bits, 0xffffffff=> -1 when signed or 4294967295 unsigned.

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

1 Comment

The answer is casting Int32 as UInt32 using the bitPattern: method! (did not know about bitPattern: method) Edit your answer to include the bitPattern thing and I'll green check ya!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.