Menu

Upper and Lower Byte of a 16bit word

Help
2011-06-06
2013-05-30
  • Marius Celliers

    Marius Celliers - 2011-06-06

    Hi. Just wanted to know if there was an easier way of getting the upper and lower byte of a 16 bit word

    I am using the following functions but still have trouble with Upper byte:

    Function HiByte(myTestWord as word) as byte
    dim myHiByte as byte
    dim TempWord as word
    TempWord = 0
    if myTestWord > 255 then
    TempWord = myTestWord & 0xff00
    myHiByte = (TempWord / 256)
    End If
    HiByte = myHiByte

    End Function

    Function LoByte(myTestWord as word) as byte
    Dim myLoByte as byte
    MyLoByte = 0
    If  myTestWord  > 255 then
    myLoByte  = myTestWord & 0xff
    Else
    myLoByte = myTestWord
    End If
    LoByte = myLoByte
    End Function

     
  • Hugh Considine

    Hugh Considine - 2011-06-06

    There are a few ways to get to the low and high bytes of a variable that are easier than those. If you have:

    Dim small As Byte
    Dim bigger As Word

    You can get the low byte like this:

    small = bigger

    or the high byte like this:

    small = bigger_H

    Another option is to create the word variable from a couple of byte variables:

    Dim bigger as word alias biggerHigh, biggerLow

    Then you can use biggerHigh and biggerLow to read or change the high and low part of the word variable respectively. You can call these byte variables anything you want, they don't need to end in High and Low. It's just the order of the variables after the "alias" that matters.

     
  • Nobody/Anonymous

    When you declare a word variable say "freqCount" GCBasic will use two locations in the register file one called freqCount for the low byte and the other called freqCount_H for the high byte so you can always get the high byte of a word variable by tacking on underscore H to whatever variable name has been used for the word.

    dim freqCount as word
    freqLo = freqCount          ' Low byte assigned to freqLo
    freqHi = freqCount_H        ' High byte assigned to freqHi
    

    Maybe you could use this instead of the functions?

     
  • Marius Celliers

    Marius Celliers - 2011-06-06

    Thank you Verry much for the quick response. I really enjoy working with  gcbasic.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.