math function to change a value 1- 8 to a bit representation


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default math function to change a value 1- 8 to a bit representation

    it late and minds going dull

    looking for a math function to change a value 1- 8 to be bit 0 - 7 representation value


    value 1 = 1 ' bit 0
    2 = 2 ' bit 1
    3 = 4 ' bit 2
    4 = 8 ' bit 3
    5 = 16 ' bit 4
    6 = 32 ' bit 5
    7 = 64 'bit 6
    8 = 128 ' bit 7
    cheers

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,380


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    see the DCD operator

  3. #3
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    DCD is exactly right, remember however that DCD sets all other bits to 0.
    If your intent is to change only a single bit: VAR.0[VALUE - 1] = 1

  4. #4
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    would it be
    if value > 2 then VAR.0[VALUE - 1]
    cos if value below 2 would be incorrect bit ??

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    This snippet (not tested) should do the trick.

    X VAR byte
    Answer VAR byte

    X = 128
    Gosub Decode
    ' variable answer should contain your bit position
    End

    Decode:
    Answer = 0
    If X < 1 then return
    Do while X <> 1
    Answer = answer + 1
    X = X > 1
    Wend
    Return

    Edited:
    Reading again your first post I realise you need the other way around:

    X = 8
    Gosub Decode
    Answer will contain the requested value
    End

    Decode:
    Answer = 0
    For i = 1 to X
    Answer = answer < 1
    Next i
    Return


    Cheers

    Al.
    Last edited by aratti; - 20th October 2014 at 10:16.
    All progress began with an idea

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    Hi,
    Code:
    X = 8
    Gosub Decode
    Answer will contain the requested value
    End
    
    Decode:
    Answer = 0
    For i = 1 to X 
    Answer = answer < 1
    Next i
    Return
    Hmmm....Answer = answer < 1
    If the above is meant to be a shift left operation then what you want is Answer = Answer << 1
    However, since Answer is set to 0 at the entry point of the routine shifting it left any number of times will still result in it being 0 when returning.

    /Henrik.

  7. #7
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    Quote Originally Posted by longpole001 View Post
    would it be
    if value > 2 then VAR.0[VALUE - 1]
    cos if value below 2 would be incorrect bit ??
    I'm a noob myself, but no... The VAR.0[] syntax is a way of addressing each bit and, while it affects the value of the byte, the bits do not interact.
    Some examples:

    VAR= %00000000 : VAR.0[0] = 1 : 'VAR NOW EQUALS %00000001
    VAR= %00000100 : VAR.0[0] = 1 : 'VAR NOW EQUALS %00000101
    VAR= %10100001 : VAR.0[1] = 1 : 'VAR NOW EQUALS %10100011
    VAR= %11111100 : VAR.0[7] = 0 : 'VAR NOW EQUALS %01111101

    As you can see by the last example, turning "off" bits is the same, only assign a 0. Reassigning a bit, already set (or cleared) has no effect.

  8. #8
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    Hmmm....Answer = answer < 1
    Just a typo! What else if not shift left.

    Cheers

    Al.
    All progress began with an idea

  9. #9
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: math function to change a value 1- 8 to a bit representation

    yep it sorted a few less if then statements

Similar Threads

  1. Logaritmic ADC representation
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th April 2010, 12:05
  2. 32 bit math
    By fnovau in forum General
    Replies: 4
    Last Post: - 13th February 2008, 00:55
  3. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 23:18
  4. 32 bit math
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 28th August 2006, 14:34
  5. not quite understanding the MATH function
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th January 2006, 21:20

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts