473,387 Members | 1,579 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.

shift operator anomaly/ambiguity when it comes to bytes?

While coding some binary data handling I found this IMHO strange behavior:

Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)

The output is "2", I expected it to be zero (outshifted bits don't come back).
The rule I determined is: whatever the shift operator S is, its real value is (S mod 8).

Is this as designed? I read the VB lanuage specification, but I cannot find anything there which would explain it. Looking at the IL
code confirms the shift value.

The real world problem I experienced wassomething like

Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)

with (b(0) << 24) not chaging anything(!) The following solution

nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)

solved the problem. What's confusing is that the AND operator apparently enforces a Byte2Integer conversion automatically

Does anybody have similar experiences/solutions/explanations?

-markus


Nov 20 '05 #1
4 2200
Nothing in the VB spec about this? Well, that's a surprise!


"Markus Hahn" <ma*********@gmx.net> wrote in message
news:HV*****************@newsread2.news.pas.earthl ink.net...
While coding some binary data handling I found this IMHO strange behavior:

Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)

The output is "2", I expected it to be zero (outshifted bits don't come back). The rule I determined is: whatever the shift operator S is, its real value is (S mod 8).
Is this as designed? I read the VB lanuage specification, but I cannot find anything there which would explain it. Looking at the IL code confirms the shift value.

The real world problem I experienced wassomething like

Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)

with (b(0) << 24) not chaging anything(!) The following solution

nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)

solved the problem. What's confusing is that the AND operator apparently enforces a Byte2Integer conversion automatically
Does anybody have similar experiences/solutions/explanations?

-markus


Nov 20 '05 #2
The specification is not so bad, but just by looking at some IL code it could definitely provide more details. Well, I guess binary
data handling is not something VB.NET was majorly intended for :)

-markus

"Jezebal" <fr****@mkkk.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Nothing in the VB spec about this? Well, that's a surprise!


"Markus Hahn" <ma*********@gmx.net> wrote in message
news:HV*****************@newsread2.news.pas.earthl ink.net...
While coding some binary data handling I found this IMHO strange behavior:

Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)

The output is "2", I expected it to be zero (outshifted bits don't come

back).
The rule I determined is: whatever the shift operator S is, its real value

is (S mod 8).

Is this as designed? I read the VB lanuage specification, but I cannot

find anything there which would explain it. Looking at the IL
code confirms the shift value.

The real world problem I experienced wassomething like

Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)

with (b(0) << 24) not chaging anything(!) The following solution

nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)

solved the problem. What's confusing is that the AND operator apparently

enforces a Byte2Integer conversion automatically

Does anybody have similar experiences/solutions/explanations?

-markus



Nov 20 '05 #3
Markus,
MSDN clearly states "Arithmetic shifts are not circular, which means the
bits shifted off one end of the result are not reintroduced at the other
end". Which appears contrary to what you are saying ;-).

http://msdn.microsoft.com/library/de...hLeftShift.asp

However! here's the kicker (later on the same page) "To prevent shifting by
more bits then the result can hold, Visual Basic masks the value of amount
with a size mask corresponding to the data type of pattern"

Section "11.18 Shift Operators" states something similar "If the value of
the second operand is greater then the number of bits in the first operand,
or is negative, then the shift amount is computes as RightOperand And
SizeMask where SizeMask is"

http://msdn.microsoft.com/library/de...BSpec11_11.asp
Is this as designed? I read the VB lanuage specification... Documented and by design!

Hope this helps
Jay

"Markus Hahn" <ma*********@gmx.net> wrote in message
news:HV*****************@newsread2.news.pas.earthl ink.net... While coding some binary data handling I found this IMHO strange behavior:

Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)

The output is "2", I expected it to be zero (outshifted bits don't come back). The rule I determined is: whatever the shift operator S is, its real value is (S mod 8).
Is this as designed? I read the VB lanuage specification, but I cannot find anything there which would explain it. Looking at the IL code confirms the shift value.

The real world problem I experienced wassomething like

Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)

with (b(0) << 24) not chaging anything(!) The following solution

nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)

solved the problem. What's confusing is that the AND operator apparently enforces a Byte2Integer conversion automatically
Does anybody have similar experiences/solutions/explanations?

-markus


Nov 20 '05 #4
Ah, now it makes sense. Thanks!
Although this masking thing is still confusing IMHO.

-markus

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:eB**************@TK2MSFTNGP11.phx.gbl...
Markus,
MSDN clearly states "Arithmetic shifts are not circular, which means the
bits shifted off one end of the result are not reintroduced at the other
end". Which appears contrary to what you are saying ;-).

http://msdn.microsoft.com/library/de...hLeftShift.asp

However! here's the kicker (later on the same page) "To prevent shifting by
more bits then the result can hold, Visual Basic masks the value of amount
with a size mask corresponding to the data type of pattern"

Section "11.18 Shift Operators" states something similar "If the value of
the second operand is greater then the number of bits in the first operand,
or is negative, then the shift amount is computes as RightOperand And
SizeMask where SizeMask is"

http://msdn.microsoft.com/library/de...BSpec11_11.asp
Is this as designed? I read the VB lanuage specification...

Documented and by design!

Hope this helps
Jay

"Markus Hahn" <ma*********@gmx.net> wrote in message
news:HV*****************@newsread2.news.pas.earthl ink.net...
While coding some binary data handling I found this IMHO strange behavior:

Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)

The output is "2", I expected it to be zero (outshifted bits don't come

back).
The rule I determined is: whatever the shift operator S is, its real value

is (S mod 8).

Is this as designed? I read the VB lanuage specification, but I cannot

find anything there which would explain it. Looking at the IL
code confirms the shift value.

The real world problem I experienced wassomething like

Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)

with (b(0) << 24) not chaging anything(!) The following solution

nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)

solved the problem. What's confusing is that the AND operator apparently

enforces a Byte2Integer conversion automatically

Does anybody have similar experiences/solutions/explanations?

-markus



Nov 20 '05 #5

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

Similar topics

45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
9
by: yuyang08 | last post by:
Dear all, Is there a logic shift operator in C++? I tried ">>", it is an arithmetic shift operator. Thanks! -Andy
16
by: Norman Diamond | last post by:
In an antique obsolete version of MFC, a CString expression could be subscripted in order to retrieve one element. Visual Studio 2005 defines CSimpleStringT::operator. At first glance it looks...
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.