473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

32-bit signed int to 16-bit unsigned int

anybody can tel me y

Int32 supervar = 1151;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 1149

same for

Int32 supervar = 21319;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 21317;

thank for any tips

Mar 9 '06 #1
7 2608
dantheman wrote:
anybody can tel me y

Int32 supervar = 1151;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 1149
Your mask constant of 0xFFFD in binary is 0b1111111111111101. Any value of
supervar that you logically AND with this mask will pass through unchanged
except that if the next-to-low-order bit is a one it will be changed to a zero.

Arithmetically interpreted, this is equivalent to subtracting 2 from supervar if
supervar mod 4 is either 2 or 3.

In your case at hand, 1151 decimal is 0x47F - it becomes 0x47D after masking
(ANDing5345), which is indeed 1149 decimal.
same for
Int32 supervar = 21319;


Yes, the same. 0x5347 becomes 0x5345 after the AND operation.

HTH,
-rick-
Mar 9 '06 #2
thanks rick

so if get this
if supervar mod 4 is either 2 or 3 i should add 2 so i will get the
poper answer of 1151 and 21319

thank you very much i appreciate it

Mar 9 '06 #3


"dantheman" wrote:
anybody can tel me y

Int32 supervar = 1151;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 1149
1151 = 0000 0100 0111 1111 binary. FFFD = 1111 1111 1111 1101 binary.

0000 0100 0111 1111
1111 1111 1111 1101 &
0000 0100 0111 1101 = 1149
same for

Int32 supervar = 21319;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 21317;


21317 = 0101 0011 0100 0111 binary. The same thing happens as before, by
anding with FFFD you're setting the 2 bit to zero, and since the number
you're anding with had it set to 1 you're subtracting two from it.
Mar 9 '06 #4
dantheman <da******@hotmail.com> wrote:
thanks rick

so if get this
if supervar mod 4 is either 2 or 3 i should add 2 so i will get the
poper answer of 1151 and 21319

thank you very much i appreciate it


It's not entirely clear what "proper answer" you're after. If you're
just trying to mask off sixteen bits, just use 0xffff instead of
0xfffd. Alternatively, use a ushort:

ushort converted = (ushort) supervar;

However, you need to decide what you want to happen if supervar is
negative.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 9 '06 #5
dantheman wrote:
thanks rick

so if get this
if supervar mod 4 is either 2 or 3 i should add 2 so i will get the
poper answer of 1151 and 21319

thank you very much i appreciate it


Well . . . what he (Jon) said. The "proper answer" depends on what the actual
problem is. I only mentioned an "arithmetic interpretation" because you framed
your original question in terms of integer results. Really it's a logical AND
operation being performed bitwise on a bunch of adjacent bits, that's all.
Normally, with a mask such as 0xFFFD the "natural" assumption would be that the
point of the AND operation is to force a particular bit to zero while leaving
all the others unchanged, rather than to perform some exotic arithmetic trick.

Your comment above about adding 2 seems to be about how to reverse the AND
operation. The more natural way to do that would be to perform a logical OR
with the inverse of your mask. In this case, OR'ing with 0x0002 would be the
logical inverse of AND'ing with 0xFFFD.

-rick-
Mar 10 '06 #6
dantheman,

What do you expect the result to be?

1151(decimal) = 0000010001111111 (binary)
FFFD(hex) = 1111111111111101 (binary)

When we perform the bitwise AND operation the result ould be
0000010001111111
1111111111111101
------------------------
0000010001111101

0000010001111101 (binary) = 1149 (decimal)

This is exaclty the result you get.

The same for the other numbers.
--
HTH
Stoitcho Goutsev (100)

"dantheman" <da******@hotmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
anybody can tel me y

Int32 supervar = 1151;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 1149

same for

Int32 supervar = 21319;

but when i do
supervar = supervar & 0xFFFD;

supervar becomes = to 21317;

thank for any tips

Mar 10 '06 #7
hi guys,

sorry for not being realy clear on this "it was a long day..."
that's exacly what i was trying to do is to "mask off sixteen bits from
the final result" so i used 0xffff instead of 0xfffd and it all works
like it should so fare...

thank you for your help

Mar 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike Ruskai | last post by:
I've searched Google every way I know how, and came up with two answers that refer to MAX_KEY and MI_MAX_KEY, along with a typedef for key_map. I have the 4.1.1 alpha source (4.0 is no good,...
20
by: Ivar | last post by:
Hi, For my supprise I found that functions have 32 parameter limit. Where to find more info about this limitation or similar limitations ? I need at least 50, 100 would be ok. Real life...
0
by: Peter D | last post by:
I have a main table and 32 sub tables linked to the main table. In those 32 tables i store info, one of those infos is : wether it is oke ( green - i store a number 3- ), wether it is not oke( -...
5
by: Ian Rutherford | last post by:
Heya guys, It seems VB .net no longer supports the awesome ability of VB 6 to declare something as a string and specify how long the string would be all in one line: Public myString as String *...
6
by: barcaroller | last post by:
I couldn't find a message-digest newsgroup, so I posted here. I have a C function that converts a string of arbitrary length to a 32-bit hash value. I realize this is overkill but I used...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
10
by: lesperancer | last post by:
you start with a small application in access97, then you have more modules and more... and you reach the point where tables like 'item' and 'employee' reach the limit and you know there's more...
3
by: Barry | last post by:
void func(int array) { for (int i = 0; i < 32; ++i) cout << array << ' '; } as the code above shows, I know int array here will decay into int *, but I still find the number 32 useful for...
6
by: docbook.xml | last post by:
I have the following in the XHTML 1.0 Strict page: <meta http-equiv="Content-Type" content="text/html;charset=utf-32" /> However W3 validator complains that "The character encoding specified in...
18
by: lak | last post by:
I am studying the Advanced programming in the unix environment. There they says that we can register upto 32 functions with atexit(). Why that is limited to 32 functions? can any one tell the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.