Indexing the bits of an Integer?

Discussion in 'VHDL' started by Takuon Soho, Mar 4, 2005.

  1. Takuon Soho

    Takuon Soho Guest

    I wanted to output a 1 or 0 signal
    based on bit number 2 of a 4 bit counter.
    i.e. if bit 2 is high, set the line high
    and if it is low, set the line low.

    Using the below code
    where my_counter is a subtype of integer (4 bit counter).
    i.e.
    subtype counter_ty is integer range 0 to 15; -- 4 bit counter,
    variable my_counter : counter_ty := 0;

    -- Next line compiles but will not sythesize
    some_signal <= conv_std_logic_vector(my_counter, 4)(2);

    The code compiles OK but refuses to synthesize
    with the message something like "complex indexes not supported".

    How can I get at the bit 2 of the counter so as to output
    a hi or low signal?? It looks like you cannot directly
    index the bit of an integer i.e. some_integer(2);

    Thanks
    Tak
    Takuon Soho, Mar 4, 2005
    #1
    1. Advertising

  2. Takuon Soho

    Neo Guest

    assign it to a temp signal and then use bit-2 of that signal.
    Neo, Mar 4, 2005
    #2
    1. Advertising

  3. On Fri, 04 Mar 2005 05:07:14 GMT, "Takuon Soho" <>
    wrote:

    >I wanted to output a 1 or 0 signal
    >based on bit number 2 of a 4 bit counter.


    >subtype counter_ty is integer range 0 to 15; -- 4 bit counter,
    >variable my_counter : counter_ty := 0;


    >How can I get at the bit 2 of the counter so as to output
    >a hi or low signal?? It looks like you cannot directly
    >index the bit of an integer i.e. some_integer(2);


    You can't. To extract a bit from something indexable, you have to use an
    indexable data type, such as one based on a vector of bits.

    Best is to include standard libraries std_logic_1164 and numeric_std,
    and to make "mycounter" of type unsigned. Then any ranges extracted from
    it are also unsigned; single bits will be of type std_logic.

    Arithmetic between unsigned (or signed) and integer works; and you can
    easily convert to the underlying std_logic_vector type whenever you need
    to.

    - Brian
    Brian Drummond, Mar 4, 2005
    #3
  4. Takuon Soho

    Takuon Soho Guest

    OK, thanks.

    I was trying to save space by using 4 bit integer subtype.

    Will use unsigned instead.

    Tak

    "Brian Drummond" <> wrote in message
    news:...
    > On Fri, 04 Mar 2005 05:07:14 GMT, "Takuon Soho" <>
    > wrote:
    >
    >>I wanted to output a 1 or 0 signal
    >>based on bit number 2 of a 4 bit counter.

    >
    >>subtype counter_ty is integer range 0 to 15; -- 4 bit counter,
    >>variable my_counter : counter_ty := 0;

    >
    >>How can I get at the bit 2 of the counter so as to output
    >>a hi or low signal?? It looks like you cannot directly
    >>index the bit of an integer i.e. some_integer(2);

    >
    > You can't. To extract a bit from something indexable, you have to use an
    > indexable data type, such as one based on a vector of bits.
    >
    > Best is to include standard libraries std_logic_1164 and numeric_std,
    > and to make "mycounter" of type unsigned. Then any ranges extracted from
    > it are also unsigned; single bits will be of type std_logic.
    >
    > Arithmetic between unsigned (or signed) and integer works; and you can
    > easily convert to the underlying std_logic_vector type whenever you need
    > to.
    >
    > - Brian
    Takuon Soho, Mar 4, 2005
    #4
  5. There may sometimes be reasons for using an integer sub-type as type for
    a counter (indexing etc.). With a small function you can get the bit
    position you were looking for:

    Library IEEE;
    use IEEE.std_logic_1164.all;
    use IEEE.numeric_std.all;
    .. . . .
    sig1 <= get_bit(your_int, your_pos);
    .. . . .
    .. . . .
    function get_bit(iv : integer; pos; integer) return std_logic;
    .. . . .
    function get_bit(iv : integer; pos; integer) return std_logic is
    constant size_c : integer := 32; -- An arbitrary value larger than
    -- the highest bit-pos you will

    -- look for
    begin
    -- assumes you count from 1 up to MSB
    if ((to_unsigned(iv, size_c) and
    to_unsigned(2 ** (pos -1), size_c)) =
    to_unsigned(0, size_c)) then
    return '0';
    else
    return '1';
    end if;
    end get_bit;

    Hope this helps,
    Charles
    Charles Gardiner, Mar 4, 2005
    #5
  6. On Fri, 04 Mar 2005 15:29:38 GMT, "Takuon Soho" <>
    wrote:

    >OK, thanks.
    >
    >I was trying to save space by using 4 bit integer subtype.
    >
    >Will use unsigned instead.


    That shouldn't save any space over using a 4-bit unsigned.
    There may be a slight difference in simulation speed, but that shouldn't
    matter until you are doing really large simulations.

    - Brian
    Brian Drummond, Mar 6, 2005
    #6
  7. Takuon Soho

    Takuon Soho Guest

    Thanks, this seems quite useful.

    Tak

    "Charles Gardiner" <> wrote in message
    news:d0aiqn$kf$04$-online.com...
    > There may sometimes be reasons for using an integer sub-type as type for a
    > counter (indexing etc.). With a small function you can get the bit
    > position you were looking for:
    >
    > Library IEEE;
    > use IEEE.std_logic_1164.all;
    > use IEEE.numeric_std.all;
    > . . . .
    > sig1 <= get_bit(your_int, your_pos);
    > . . . .
    > . . . .
    > function get_bit(iv : integer; pos; integer) return std_logic;
    > . . . .
    > function get_bit(iv : integer; pos; integer) return std_logic is
    > constant size_c : integer := 32; -- An arbitrary value larger than
    > -- the highest bit-pos you will
    > -- look for
    > begin
    > -- assumes you count from 1 up to MSB
    > if ((to_unsigned(iv, size_c) and
    > to_unsigned(2 ** (pos -1), size_c)) =
    > to_unsigned(0, size_c)) then
    > return '0';
    > else
    > return '1';
    > end if;
    > end get_bit;
    >
    > Hope this helps,
    > Charles
    Takuon Soho, Mar 7, 2005
    #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. C
    Replies:
    0
    Views:
    498
  2. GGG
    Replies:
    10
    Views:
    12,556
    Donar
    Jul 6, 2006
  3. Emin
    Replies:
    4
    Views:
    410
    Paul McGuire
    Jan 12, 2007
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,103
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    219
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page