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 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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)
|
by: Richard Hayden |
last post by:
Hi,
I have the following code for example:
/**********************************/
#include <iostream>
class C1 {
public:
|
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...
|
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...
|
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...
|
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:
|
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
|
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...
|
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>>
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |