PDA

View Full Version : LOWRD & HIWRD



marcuslee
10-09-2008, 05:04
I have read descriptions of LOWRD and HIWRD on the net. I understand some of it but not all. I know it has something to do with converting between 16-bit and 32-bit data types.

Here is a list of things I understand and don't understand about this subject:

I understand what a bit is and what a byte is.
I understand what a word is.
I don't completely understand the difference between 16-bit, 32-bit and 64-bit systems except that 32 is twice 16 and 64 is quad 16. (word, dword, and quad ... I think.)


But, I'm not sure if I will understand anything y'all have to say either. So, I'm not sure what I expect to get out of this. If anyone has a simple explanation for how LOWRD, HIWRD work and can explain it in plain English that a non-programmer might understand ... then, I might grasp this subject.

Who is up to the challenge?

Mark ??? :-[

ErosOlmi
10-09-2008, 07:53
I'm not an expert but, remain simple.

All numbers can be divided in 2 pieces, left and right part.
By convention, the left part is usually defined as MSB (most significant bits) while the right part is defined as LSB (least significant bit). But you can find the other way round.

Reference to http://www.thinbasic.com/public/products/thinBasic/help/html/numericvariables.htm looking at "Integer data types".
An INTEGER or a WORD number (16 bits) can contains 2 BYTEs (8 bits each) one on the left and one on the right.
One 16 bits number [0000000000000000]
Two 8 bits number [00000000][00000000]

Again a LONG or DWORD number (32 bits) can contains 2 INTEGERs or WORD (16 bits each), again one on the left and one on the right.
One 32 bits number [00000000000000000000000000000000]
Two 16 bits number [0000000000000000][0000000000000000]

LOWRD and HIWRD are used to get the left or the right part of a number.
If input is a 32 bit, output will be 16 bit (left or right)

Hope I was clear.
Eros

Charles Pegge
10-09-2008, 10:22
How these functions work:

LOWRD

To obtain the 16 bit LOWRD of a 32 bit DWORD or LONG integer you simply mask off the upper 16 bits. This is done by using a bitwise AND like this:

myWord=myDword and &h0000FFFF

this is the same as:

myLoWord=myDword and 65535


HIWRD

To Get the HIWRD, simply do an integer division by &h10000 or 65536.

MyHiWord=myDword \ &h10000

This is not the most efficient way of doing it in machine terms. Many languages support bit shifting. You need to shift the bits 16 places to the right. The 16 lower bits then fall off the end, leaving the upper bits in the right place. This is about 40 times quicker for the CPU.

Furthermore, integer division using negative LONG integers, will result in the sign messing up the result. - Should be okay with DWORDS though.

marcuslee
10-09-2008, 16:20
For those of you that saw the original message, I am sorry for misspelling LOWRD & HIWRD. My eyes must be going bad, my brain adding letters like that. (I had originally spelled it LOWORD & HIWORD.)

I think I understand Eros' explanation. Thankfully, this isn't one of those things that I will have to do myself. But, it is nice to get explantions.

Mark