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...

jww
  • 97,681
  • 90
  • 411
  • 885
Jono Tho'ra
  • 1,476
  • 3
  • 18
  • 28
  • What have you tried? Why is this a Swift/Objective-C question (unless you show code, this is a general numerical question)? – GoZoner Dec 18 '14 at 22:23

1 Answers1

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.

Community
  • 1
  • 1
  • 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! – Jono Tho'ra Dec 19 '14 at 00:44