Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
10 years ago

How to pad or extend the most significant bit (bit 23) into bits 24 through 31

Hi,

As suggested by forumer kflynn (http://www.alteraforum.com/forum/member.php?u=86819) in this link (http://www.alteraforum.com/forum/showthread.php?t=48398), the below issue can be resolved by padding or extending the most significant bit (bit 23) into bits 24 through 31.

I want to know, how could I extend the most significant bit (bit 23) into bits 24 through 31? How could I do that in C code? I am using C code to program Nios II.

I was thinking of using bit shifting operation but not knowing in details how by using bit shifting operation, the above could be achieved, any link or resource is much appreciated.

Thank you in advance.

HI, I have a very simple question but I have no idea what went wrong. Basically I have a 24-bit signed signal from VHDL block fed into Nios II system. I use C code for NIos II system. This is my code:Code:

value =IORD_ALTERA_AVALON_PIO_DATA(base); 

to read the 24-bit signed signal. I define it asCode:

alt_32 value=0; 

The problem is it always give me positive value every time read from this line even though I am sure it is negative value from VHDL code.Code:

printf("\n value= %d \n", value);                                                                                   

Any idea what went wrong??? sorry for the simple question... Thank you in advance

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    the msb is the sign bit, so you could check your 24'th bit using a bitmask, if it is set, change the 32'nd bit and vice versa. Then clear the 24'th bit.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    since you are using Nios, this operation could be easily converted to simple custom instruction, something like:

    
    module ci_pad(
        input  dataa,
        output  result
    );
    assign result = { {8{dataa}}, dataa};
    endmodule
    

    alternatively, as PietervanderStar suggested (in C):

    
    value = (value & (0x1 << 24)) ?
        value | (0xFF << 24 ) :
        value;
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I think that C code should actually be:

    
        value = (value & (0x1 << 23)) ?
                 value | (0xFF << 24 ) :
                 value;
    

    Notice that you are checking bit 23 to see if it is a 1 or a 0 (not bit 24!).