Need some help with bit operations

  • Thread starter Peter van der Veen
  • Start date
P

Peter van der Veen

Hi

I have the following problem.
In a file a have 2 byte value, but the first 10 Bits are a value and
the last 3 also.

I thought i read them as an UINT16 value and than do some shifting,
but VB.NET only allows this for integers and shorts.
If i read in the valu as a short and do the shifting then i got the
wrong value.
How to solve?

The code

Dim time As Short
time = r.ReadInt16
time = time >> 3

This gives the wrong value.



Dim time As UINT16
time = r.ReadUInt16
time = time >> 3 <this gives an error
 
M

Mattias Sjögren

I thought i read them as an UINT16 value and than do some shifting,
but VB.NET only allows this for integers and shorts.
If i read in the valu as a short and do the shifting then i got the
wrong value.
How to solve?

Why don't you store the value in an Integer first (the lower 16 bits
of it) and then do the shifting.



Mattias
 
M

Mohamed El Ashmawy

I think the fact that the highest bit of short would be considered a sign
bit could cause a problem.


Try the following to make the extracting
Dim time As Integer
time = 44204 (This number is 1010 1100 1010 1100)
Dim HighThreeBits As Integer = time >> 13
(will return 5 (101))
Dim ThirteenLowBits As Integer = (time And 8191) (will
return 3244 (0 1100 1010 1100))


Note that 8191 is 1 1111 1111 1111

This should resolve your problem

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top