Who can explain the bit'pos for me?

Discussion in 'VHDL' started by threeinchnail@gmail.com, Jun 28, 2006.

  1. Guest

    Hello everybody, I have question here about bit'pos. I saw this
    function from a place.

    function bt_to_natural(bv: in bit_vector) return natural is
    variable result : natural:=0;
    begin
    for index in bv'range loop
    result :=result*2+bit'pos(bv(index));
    end loop;
    return result;
    end bv_to_natural;

    I am searching the bit'pos on the internet. And I found one
    explaination is :
    signal 'pos is return the position (number) of the input.
    bit' pos('0') would return 0; bit'pos('1') would return 1;
    I am a little bit confused about it. I think 'pos should be the
    position of the bit. But I take my definition into above function. I
    cannot figure it out.

    Is there anybody to help me figure it out? Could you give a real
    example for me? For example how to convert 10101010 to the integer.
    Many thanks.

    Zhi
    , Jun 28, 2006
    #1
    1. Advertising

  2. Hubble Guest

    schrieb:

    > Hello everybody, I have question here about bit'pos. I saw this
    > function from a place.


    Consult the VHDL LRM (Language Reference Manual)


    http://www.microlab.ch/courses/vlsi/vhdl-ieee/TUTORIAL/IEEE/HTML/1076_14.HTM#14.1

    bit is an enumaration type:

    type BIT is ('0', '1'); -- package standard

    So bit'pos is a way to convert the type bit to integer:

    bit'pos('0')=0
    bit'pos('1')=1

    Attrributs pos and val can be used to convert enumaration types:
    variable c: character;#
    variable b: bit;
    variable i: integer;

    c:=character'val(bit'pos(b)+character'pos('0')); --
    character'pos('0')=48 in ASCII character set
    b:=bit'val(character'pos(c)-character'pos('0'));

    ('val is the opposite of 'pos)

    Hubble.
    Hubble, Jun 29, 2006
    #2
    1. Advertising

  3. Guest

    Thank you, Hubble. I understand your explanation..
    But I am still confused it a little bit. If I take an example of
    bit_vector ("10101010"). I want to convert it to integer. According to
    the function:
    1. result:=0+0;
    2 result:=2*0+1;
    3 result:=2*1+0;
    .........................Does it like that? I think my way is wrong.
    Because It definitedly cannot get the expected result.
    Could you give me a clear example like that ? Since the function can
    convert bitvector to natural, how does it work? Thanks again.

    Hubble wrote:
    > schrieb:
    >
    > > Hello everybody, I have question here about bit'pos. I saw this
    > > function from a place.

    >
    > Consult the VHDL LRM (Language Reference Manual)
    >
    >
    > http://www.microlab.ch/courses/vlsi/vhdl-ieee/TUTORIAL/IEEE/HTML/1076_14.HTM#14.1
    >
    > bit is an enumaration type:
    >
    > type BIT is ('0', '1'); -- package standard
    >
    > So bit'pos is a way to convert the type bit to integer:
    >
    > bit'pos('0')=0
    > bit'pos('1')=1
    >
    > Attrributs pos and val can be used to convert enumaration types:
    > variable c: character;#
    > variable b: bit;
    > variable i: integer;
    >
    > c:=character'val(bit'pos(b)+character'pos('0')); --
    > character'pos('0')=48 in ASCII character set
    > b:=bit'val(character'pos(c)-character'pos('0'));
    >
    > ('val is the opposite of 'pos)
    >
    > Hubble.
    , Jun 29, 2006
    #3
  4. D Stanford Guest

    wrote:
    > Thank you, Hubble. I understand your explanation..
    > But I am still confused it a little bit. If I take an example of
    > bit_vector ("10101010"). I want to convert it to integer. According to
    > the function:
    > 1. result:=0+0;
    > 2 result:=2*0+1;
    > 3 result:=2*1+0;


    For it to function properly, bv'range must be max downto 0. It must be
    sequencing from MSB to LSB.

    with an input bit vector of "1100"

    1. index = 0, result = 0*2 + 1 = 1
    2. index = 1, result = 1*2 + 1 = 3
    3. index = 2, result = 3*2 + 0 = 6
    4. index = 3, result = 6*2 + 0 = 12
    D Stanford, Jun 29, 2006
    #4
  5. Guest

    I got it. Thank you all very much ^_^

    D Stanford wrote:
    > wrote:
    > > Thank you, Hubble. I understand your explanation..
    > > But I am still confused it a little bit. If I take an example of
    > > bit_vector ("10101010"). I want to convert it to integer. According to
    > > the function:
    > > 1. result:=0+0;
    > > 2 result:=2*0+1;
    > > 3 result:=2*1+0;

    >
    > For it to function properly, bv'range must be max downto 0. It must be
    > sequencing from MSB to LSB.
    >
    > with an input bit vector of "1100"
    >
    > 1. index = 0, result = 0*2 + 1 = 1
    > 2. index = 1, result = 1*2 + 1 = 3
    > 3. index = 2, result = 3*2 + 0 = 6
    > 4. index = 3, result = 6*2 + 0 = 12
    , Jun 29, 2006
    #5
  6. john Doef Guest

    D Stanford a écrit :

    > wrote:
    > > Thank you, Hubble. I understand your explanation..
    > > But I am still confused it a little bit. If I take an example of
    > > bit_vector ("10101010"). I want to convert it to integer. According to
    > > the function:
    > > 1. result:=0+0;
    > > 2 result:=2*0+1;
    > > 3 result:=2*1+0;

    >
    > For it to function properly, bv'range must be max downto 0. It must be
    > sequencing from MSB to LSB.

    It works with any range, doesn't it ?

    JD.
    john Doef, Jun 29, 2006
    #6
  7. D Stanford Guest


    > > For it to function properly, bv'range must be max downto 0. It must be
    > > sequencing from MSB to LSB.

    > It works with any range, doesn't it ?
    >
    > JD.


    A good point. It would have been better stated max downto min instead
    of 0.
    D Stanford, Jun 29, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,746
    Timothy Bendfelt
    Jan 19, 2007
  2. Hank Gong
    Replies:
    0
    Views:
    341
    Hank Gong
    Apr 12, 2007
  3. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  4. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
  5. prati
    Replies:
    0
    Views:
    442
    prati
    Oct 27, 2012
Loading...

Share This Page