Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
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
?
529 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
  • 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

Industry Leaders: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

572 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