Sign extend 16 bit to 32 bit

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Sign extend 16 bit to 32 bit

 
0
  #1
Oct 19th, 2008
I am trying to write a program that either zero extends or sign extends a 16 bit input to a 32 bit output, what exactly is meant by sign extending a number? I believe zero extending would be adding 16 0s in the 16 Most Significant bits, Is sign extending just keeping the most significant bit as the sign?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 43
Reputation: shrughes is an unknown quantity at this point 
Solved Threads: 4
shrughes shrughes is offline Offline
Light Poster

Re: Sign extend 16 bit to 32 bit

 
1
  #2
Oct 19th, 2008
Sign-extending means the value of the most significant bit of the 16-bit integer (the sign bit, for signed 16-bit integers) is used to fill the 16 higher bits.

0abcdefghijklmno => 00000000000000000abcdefghijklmno
1abcdefghijklmno => 11111111111111111abcdefghijklmno

This means that if your 16 bits represent a signed integer, your 32-bit value will represent the same integer.

When you write

int16_t x = -5;
int32_t y = x;

the value gets cast up to a 32-bit signed integer using the sign extension described above. If zero extension were used instead, y would be assigned the value 65531, instead of -5.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 19
Reputation: ssDimensionss is an unknown quantity at this point 
Solved Threads: 0
ssDimensionss ssDimensionss is offline Offline
Newbie Poster

Re: Sign extend 16 bit to 32 bit

 
0
  #3
Apr 25th, 2009
Hi any ideas on how to do this using C?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

Re: Sign extend 16 bit to 32 bit

 
0
  #4
Apr 25th, 2009
C decides whether to sign extend or zero-extend when you cast an integer type based on whether it's signed or unsigned.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 19
Reputation: ssDimensionss is an unknown quantity at this point 
Solved Threads: 0
ssDimensionss ssDimensionss is offline Offline
Newbie Poster

Re: Sign extend 16 bit to 32 bit

 
0
  #5
Apr 26th, 2009
well the thing is im writing a code to take a hexadecimal input which is a 32 bit instruction. The last 16bits of this instruction is a constant which i have to add to another value. But apparently i can't add it because its 16bits long and im adding to 32 bit register value. Just wondering if there's a way to make this 16bits long into 32 bits long ? Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Computer Science Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC