473,385 Members | 1,641 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,385 software developers and data experts.

Strange results in bitwise operations

Hi all,
I'm doing some bitwise operations in VB.NET, and I got some strange results.
If an Int16 (Short) variable has the value &HAA (8 bits) and I do a 4-bit
shift:

var16 = var16 << 4

I got, correctly, the value &HAA0. But, if I try to do a 8-bit shift, I got
&HFFFFAA00 !!!!
I did many tests and it appears that I need a 32-bit variable to do
operations that normally - in other platforms - I can do in 16-bit
variables!
Does someone knows why? I've lost some hours of my life to outline - but not
solve - this, and I'd like to know, at least, why.

Thanks in advance,
Fabrício de Novaes Kucinskis.
Nov 16 '05 #1
4 1068
Fabrício de Novaes Kucinskis wrote:
Hi all,
I'm doing some bitwise operations in VB.NET,
Odd that you posted to a C# group, a C++ group, but no VB.NET group...
and I got some strange
results. If an Int16 (Short) variable has the value &HAA (8 bits) and
I do a 4-bit shift:

var16 = var16 << 4

I got, correctly, the value &HAA0. But, if I try to do a 8-bit shift,
I got &HFFFFAA00 !!!!
This is because Int16 is a signed type, so .NET sign-extends that value when
it's converted to 32-bits. Note that an Int16 can't hold 0xFFFFAA00 - the
sign-extension is being done when the value is converted to an Int32, most
likely on the way to being converted to a string (I assume you're displaying
it in a window, or converting it to a string, etc).
I did many tests and it appears that I need a 32-bit variable to do
operations that normally - in other platforms - I can do in 16-bit
variables!
Does someone knows why? I've lost some hours of my life to outline -
but not solve - this, and I'd like to know, at least, why.


The bits behave the same way they always have - you're just not accounting
for sign-extensions when converting to a 32-bit type.

-cd
Nov 16 '05 #2
Hi, Carl. I'm sorry for the group's mistake.

I'm not converting my values to 32-bit, neither showing or converting then
to strings.
I'm working directly with the numeric vars, and I got the values in the
watch window.

A simple example of what is happening here:

dim var16 as Int16 '(or short)
var16 = &HAA
var16 = CType(var16 << 4, Int16) 'result expected: &HAA0 - ok!
var16 = &HAA
var16 = CType(var16 << 8, Int16) 'result expected: &HAA00; result:
&HFFFFAA00

I'm not sure, but I think there's no 32-bit convertion here. Or there is?
Thanks for the help.

Best regards,
Fabrício.

and I got some strange
results. If an Int16 (Short) variable has the value &HAA (8 bits) and
I do a 4-bit shift:

var16 = var16 << 4

I got, correctly, the value &HAA0. But, if I try to do a 8-bit shift,
I got &HFFFFAA00 !!!!
This is because Int16 is a signed type, so .NET sign-extends that value

when it's converted to 32-bits. Note that an Int16 can't hold 0xFFFFAA00 - the
sign-extension is being done when the value is converted to an Int32, most
likely on the way to being converted to a string (I assume you're displaying it in a window, or converting it to a string, etc).
I did many tests and it appears that I need a 32-bit variable to do
operations that normally - in other platforms - I can do in 16-bit
variables!
Does someone knows why? I've lost some hours of my life to outline -
but not solve - this, and I'd like to know, at least, why.


The bits behave the same way they always have - you're just not accounting
for sign-extensions when converting to a 32-bit type.

Nov 16 '05 #3
Fabrício de Novaes Kucinskis wrote:
Hi, Carl. I'm sorry for the group's mistake.

I'm not converting my values to 32-bit, neither showing or converting
then to strings.
I'm working directly with the numeric vars, and I got the values in
the watch window.

A simple example of what is happening here:

dim var16 as Int16 '(or short)
var16 = &HAA
var16 = CType(var16 << 4, Int16) 'result expected: &HAA0 - ok!
var16 = &HAA
var16 = CType(var16 << 8, Int16) 'result expected: &HAA00; result:
&HFFFFAA00

I'm not sure, but I think there's no 32-bit convertion here. Or there
is? Thanks for the help.


The debugger (or the .NET runtime) is widening it to 32 bits during the
conversion to string for display in the watch window. Nothing to be alarmed
about.

-cd
Nov 16 '05 #4
Ok, so there is a string convertion! Thanks a lot!

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eh**************@tk2msftngp13.phx.gbl...
Fabrício de Novaes Kucinskis wrote:
Hi, Carl. I'm sorry for the group's mistake.

I'm not converting my values to 32-bit, neither showing or converting
then to strings.
I'm working directly with the numeric vars, and I got the values in
the watch window.

A simple example of what is happening here:

dim var16 as Int16 '(or short)
var16 = &HAA
var16 = CType(var16 << 4, Int16) 'result expected: &HAA0 - ok!
var16 = &HAA
var16 = CType(var16 << 8, Int16) 'result expected: &HAA00; result:
&HFFFFAA00

I'm not sure, but I think there's no 32-bit convertion here. Or there
is? Thanks for the help.
The debugger (or the .NET runtime) is widening it to 32 bits during the
conversion to string for display in the watch window. Nothing to be

alarmed about.

-cd

Nov 16 '05 #5

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

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
8
by: Fabrício de Novaes Kucinskis | last post by:
Hi all, I'm doing some bitwise operations in VB.NET, and I got some strange results. If an Int16 (Short) variable has the value &HAA (8 bits) and I do a 4-bit shift: var16 = var16 << 4 I...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
8
by: Skeleton | last post by:
I am using Windows XP + PHP 5.1.2 at home for development and using Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise operations. While doing this, I have come accross to...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.