Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12589 Discussions

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

Altera_Forum
Honored Contributor II
1,497 Views

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
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
776 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
776 Views

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;
0 Kudos
Altera_Forum
Honored Contributor II
776 Views

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!).
0 Kudos
Reply