www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to convert a ubyte to a byte without losing information?

reply "CharmingChocolate" <dsdf sfsdf.com> writes:
I can cast a ubyte to a byte, but as far as I know I'd only get 
positive values out of that conversion. If I was instead getting 
a ubyte out of a function and I want to assign those bits to a 
byte and have some of those numbers be interpreted as negative 
values by the new type they're in, how would I cast that in D?
Aug 16 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 16 August 2015 at 11:43:24 UTC, CharmingChocolate 
wrote:
 I can cast a ubyte to a byte, but as far as I know I'd only get 
 positive values out of that conversion.
ubyte b = 255; byte b2 = cast(byte) b; assert(b2 == -1); No information is lost by that cast, all the bits remain exactly the same.
Aug 16 2015