?
Solved

convert 16 bit number to 32 bit sign extended number and how to perform <<2 operation

Posted on 2005-04-02
3
Medium Priority
?
527 Views
Last Modified: 2008-02-01
hello!

can you show me how will i do the following in c++:

how will i convert 16 bit number to a  32 bit sign extended number.
also, how will i perform <<2 operation.


regards,
siddharth
0
Comment
Question by:sjain1203
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13688569
Um, usually, you would

#ifdef MSC_VER // MS compilers use different type names
typedef __int16 int16_t;
typedef __int32 int32_t;
#endif

int16_t i16 = 42;
int32_t i32 = reinterpret_cast<int32_t>(i16);

>>also, how will i perform <<2 operation.

That depends on what you want to do. Can you elaborate? E.g.

int n = 1 << 2; // n will hold 4 now
0
 

Author Comment

by:sjain1203
ID: 13688634
i get an error: " cannot cast from 'short' to 'int' when i execute the above code.

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13688657
Sorry, my fault, that should be

#ifdef _MSC_VER // MS compilers use different type names
typedef __int16 int16_t;
typedef __int32 int32_t;
#endif

int16_t i16 = 42;
int32_t i32 = static_cast<int32_t>(i16);

BTW, you could also use a "C-style" cast to achieve the same:

int16_t i16 = 42;
int32_t i32 = (int32_t)i16;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question