Endiannes & Splitting Values

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 6 14:58:25 PDT 2016


On Wednesday, 6 July 2016 at 21:44:37 UTC, BitGuy wrote:
> I'm trying to implement a feistel cipher that'll give the same 
> results regardless of the endianness of the machine it runs on. 
> To make the cipher I need to split a 64bit value into two 32bit 
> values, mess with them, and then put them back together. I can 
> think of a few ways to split a 64bit value with versions or the 
> endianness functions in bitmanip but it all seems pretty messy 
> for just wanting to split a value... I'm thinking maybe I can 
> just cast and bitshift so I can forget about the endianness but 
> I'm not really sure about the casting down rules and if that'd 
> work?

You can just use bitwise operations, without casts (with Value 
Range Propagation, the compiler knows that the two halves fit a 
uint each). The recomposition will require a cast, however.

ulong value = ...
uint low = value & uint.max;
uint high = value >>> 32;
assert(value == ((cast(ulong)high << 32) | low));




More information about the Digitalmars-d-learn mailing list