473,385 Members | 1,353 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.

Translating a bitwise operation from C++

I'm trying to generate a 4-byte unsigned integer in VB2005 that
encodes a number of bit-level variables. What I need to do is to be
able to compute a value for the uint that is based on specific values
for the constituent variables; the unit can then be passed to an
external DLL, which requires a function parameter in this form.

Unfortunately the only documentation I have for the external DLL is in
(I think) C++, which I'm struggling to understand. The C++ code for
the procedure to create a value for the number is:

MakeNumber(CV, DU, DT, ST, SID)

( ((CV) & 0x1F) | (((DU) & 0x1F)<<5) | (((DT) & 0x1F)<<10) |

(((ST) & 0xFF)<<16) | (((SID) & 0xFF)<<24) )

(The 3 lines above are all on a single line in the original code.)

So, the procedure is encoding values for the 5 variables CV, DU, DT,
ST, SID) in assembling the final uint.

Now I can see the gist of what's being done here and can spot the
bitwise operators but I've got 2 questions about how to translate this
procedure into VB2005:

1. Is there some equivalent assembly procedure that I can use in
VB2005 and if so what would the exact syntax be? For example is there
any equivalent of what seems to be the | operator?

2. What data type should I use for the values held in CV, DU etc?
UShort maybe?

Many thanks for any help.
May 27 '07 #1
4 1852
The data 0xff is the same as 11111111 in binrary and would be a single byte
so the data type is byte or Char or short I think, the operators convert
something like this:

| = OR
& = AND
<< = Shift bit left (probably a function for this in VB.net)

In C++ a single operator means a bitwise action to do on a value and a
double operator is used in logical comparsions the following below would be
used in if statements to compare things:

||
==
&&

Regards,

Mike.

"John Dann" <ne**@prodata.co.ukwrote in message
news:p9********************************@4ax.com...
I'm trying to generate a 4-byte unsigned integer in VB2005 that
encodes a number of bit-level variables. What I need to do is to be
able to compute a value for the uint that is based on specific values
for the constituent variables; the unit can then be passed to an
external DLL, which requires a function parameter in this form.

Unfortunately the only documentation I have for the external DLL is in
(I think) C++, which I'm struggling to understand. The C++ code for
the procedure to create a value for the number is:

MakeNumber(CV, DU, DT, ST, SID)

( ((CV) & 0x1F) | (((DU) & 0x1F)<<5) | (((DT) & 0x1F)<<10) |

(((ST) & 0xFF)<<16) | (((SID) & 0xFF)<<24) )

(The 3 lines above are all on a single line in the original code.)

So, the procedure is encoding values for the 5 variables CV, DU, DT,
ST, SID) in assembling the final uint.

Now I can see the gist of what's being done here and can spot the
bitwise operators but I've got 2 questions about how to translate this
procedure into VB2005:

1. Is there some equivalent assembly procedure that I can use in
VB2005 and if so what would the exact syntax be? For example is there
any equivalent of what seems to be the | operator?

2. What data type should I use for the values held in CV, DU etc?
UShort maybe?

Many thanks for any help.

May 27 '07 #2
"John Dann" <ne**@prodata.co.ukwrote in message
news:p9********************************@4ax.com...
MakeNumber(CV, DU, DT, ST, SID)

( ((CV) & 0x1F) | (((DU) & 0x1F)<<5) | (((DT) & 0x1F)<<10) |

(((ST) & 0xFF)<<16) | (((SID) & 0xFF)<<24) )
(CV And &h1f) or ((DU and &h1f) * 32) or ((DT And &h1f) * 1024) or ((ST And
&hff) * 65536) or ((SID And &hff) * 16777216)

You might have issues with the last bracketed section because I presume this
will default to signed int. You might need to convert them to unsigned int
and then back.
2. What data type should I use for the values held in CV, DU etc?
UShort maybe?
It is a macro so not specified. Most likely they used 32 bit int.

Michael
May 28 '07 #3

On Mon, 28 May 2007 11:03:16 +1000, "Michael C" <no****@nospam.com>
wrote:
>(CV And &h1f) or ((DU and &h1f) * 32) or ((DT And &h1f) * 1024) or ((ST And
&hff) * 65536) or ((SID And &hff) * 16777216)

You might have issues with the last bracketed section because I presume this
will default to signed int. You might need to convert them to unsigned int
and then back.
>2. What data type should I use for the values held in CV, DU etc?
UShort maybe?

It is a macro so not specified. Most likely they used 32 bit int.
Thanks for the response. Actually, I do have a bit more info on the
data types in the original - just didn't include it in the original
question in the interests of brevity (and also because I didn't fully
understand it!). The relevant details are below. (These details define
the memory structure that the number I'm trying to generate needs to
access, so the bit structures of the two integers must presumably
mirror one another.)

struct VarA // the same size as a UINT
{
unsigned int CV: 5;
unsigned int DU: 5;
unsigned int DT: 5;
unsigned int reserved : 1;

unsigned int ST : 8;
unsigned int SID: 8;

bool IsCurrentVariable(); // is the ID valid for current data?
bool IsHiLowVariable(); // is ID valid for Hi/Low data?

};

union ID
{
DWORD id;
VarA fields;
};

I've been able to make some progress in getting this working in that
the code for creating the 4-byte uint compiles and the resulting
number does return a value when passed in the function call, but
unfortuantely it's not the right value! So I still have work to do and
any further ideas would be very helpful.

The structure of my code at present is:

Dim result as ULong
Dim CV, DU, DT, ST, SID as UShort

result = _
(CV And &h1f) or ((DU and &h1f) * 32) or ((DT And &h1f) * 1024) or _
((ST And &hff) * 65536) or ((SID And &hff) * 16777216)

(I've also tried declaring CV etc as UInteger but no change - result
is identical and still wrong.)
I wasn't sure exactly what was meant by:
>You might have issues with the last bracketed section because I presume this
will default to signed int. You might need to convert them to unsigned int
and then back.
Could someone spell out just what might need converting please, ie the
whole result value or the last bracketed part?
May 28 '07 #4
"John Dann" <ne**@prodata.co.ukwrote in message
news:31********************************@4ax.com...
Thanks for the response. Actually, I do have a bit more info on the
data types in the original - just didn't include it in the original
question in the interests of brevity (and also because I didn't fully
understand it!). The relevant details are below. (These details define
the memory structure that the number I'm trying to generate needs to
access, so the bit structures of the two integers must presumably
mirror one another.)
That makes sense although I'm not sure you could duplicate this in vb.net.
You could try using the FieldOffset attribute.
I've been able to make some progress in getting this working in that
the code for creating the 4-byte uint compiles and the resulting
number does return a value when passed in the function call, but
unfortuantely it's not the right value! So I still have work to do and
any further ideas would be very helpful.

The structure of my code at present is:

Dim result as ULong
Dim CV, DU, DT, ST, SID as UShort

result = _
(CV And &h1f) or ((DU and &h1f) * 32) or ((DT And &h1f) * 1024) or _
((ST And &hff) * 65536) or ((SID And &hff) * 16777216)

(I've also tried declaring CV etc as UInteger but no change - result
is identical and still wrong.)
Everything should be uinteger I believe (int is 32bit in VB I presume). To
find the problem try giving CV a non-zero value and everything else a zero
value. Do you get the expected result? If so then try the same thing for DU
and then DT etc.
I wasn't sure exactly what was meant by:
>>You might have issues with the last bracketed section because I presume
this
will default to signed int. You might need to convert them to unsigned int
and then back.

Could someone spell out just what might need converting please, ie the
whole result value or the last bracketed part?
With vb it will default to signed integer in most cases. This can cause
problems when doing math like this because the most significant bit is the
sign. To avoid errors you need to define everything as unsigned int. Eg:
&hffff * &h10000 should give &hffff0000 but will give an error if unsigned
is not used.
May 29 '07 #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...
2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
7
by: Jerry | last post by:
I want an algorithm that do arithmetic operations(divide,mutiply,add etc.)just using bitwise operators:<<,>>,&,|,^; For example,how "a/10" can be implemented. I just want a hint. Thanks.
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
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...
10
by: David R. | last post by:
I want to do bitwise operation on some large integers. For example, Response.Write CBool(2 AND 2^30) ' returns False Response.Write CBool(2 AND 2^31) ' CRASHED! Looks like the AND...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
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...
3
by: Sakhtkoosh | last post by:
I have written a program in C environment. I need to do operation on codes with more than 64 elements whose values are 0 and 1. At first, I simulated it with an array in 2 dimension(n*m) but after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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:
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
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 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.