java - Storing unsigned long value in two 16bit register -


i want store unsigned long value in 2 16bit register.for example if have long value (-2,147,483,648 2,147,483,647) i'm using formula like:

v[0] = myvalue % 65536 v[1] = myvalue / 65536 

to value register

outval = reg0 + (reg1 * 65536) 

but how unsigned long value range 0 4,294,967,295?

as commenter harold pointed out already, formula doesn't work correctly negative numbers.

you should bitwise instead of using math avoid surprises (and speed things in case compiler didn't optimize already).

splitting:

v[0] = myvalue & 0xffff v[1] = myvalue >> 16    // implicitly cuts off lower 16 bits                         // shifting them away nirvana 

joining:

outval = reg0 | (reg1 << 16) 

this applies both signed , unsigned (provided variables have same "sign type").


legend, in case language (which didn't specify) uses different operators:

& bitwise and, | bitwise or, << , >> bitwise shifting left/right (shl/shr), 0x marks hexadecimal literal (you use 65536 instead of 0xffff, think hex literal makes clearer magic number comes from).


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -