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

Shift Bits of Unsigned Int

Hello VB Gurus,

I have an unusual requirement to shift an unsigned int right one bit:

Dim myVar As UInt32 = UInt32.Parse("123456")
Dim myResult As UInt32
myResult = myVar >> 1

However, the >> operator only works on Byte, Short, Integer, and Long.

I also tried to convert to binary and manually shift the bits, but I only
found ways to go from binary to string--not back to binary.

Does anyone know how this could be done? (I'm not too concerned about
performance, so even resource-killing string manipulation would be fine with
me.)

Thank you,

Marty
Nov 20 '05 #1
7 2887
Marty,

Well, there are a few things you can do.
First, do you "really" need the resulting internal value to be a UInt32?
Since UInt's are not CLS compliant, and currently are not supported in VB, I
urge you to not use them.
Could an Int32 or Int64 work?

In this particular example, since you are shifting by one bit to the right, your
maximum possible value would by nature end up being that of a Signed Int32 I
believe.
In this case, you could do the following:

Dim myVar As Int64 = Int64.Parse("123456")
Dim myResult As Int32

myResult = CInt(myVar >> 1)

With that said, I realize that maybe you do "need" for it to remain as a UInt32.
IMHO, the fastest and easiest way to deal with this until VB 2005, would be to
write a very small function in a C# DLL and add a reference to that in your VB
project. However, keep in mind that passing UInt's between assemblies is not
recommended due to the non-CLS compliance. Sure it works, just not recommended.

Possibly the "better" easy way would be to just cast everything to Int64
internally, and then back to UInt32 to store values back out, if this is
required.

Although making a general recommendation that would work for you overall
implementation would require me to know more about what you really need to
achieve.

Gerald
"Marty McFly" <Ma***@Mc.Fly> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
Hello VB Gurus,

I have an unusual requirement to shift an unsigned int right one bit:

Dim myVar As UInt32 = UInt32.Parse("123456")
Dim myResult As UInt32
myResult = myVar >> 1

However, the >> operator only works on Byte, Short, Integer, and Long.

I also tried to convert to binary and manually shift the bits, but I only
found ways to go from binary to string--not back to binary.

Does anyone know how this could be done? (I'm not too concerned about
performance, so even resource-killing string manipulation would be fine with
me.)

Thank you,

Marty

Nov 20 '05 #2

"Marty McFly" <Ma***@Mc.Fly> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
Hello VB Gurus,

I have an unusual requirement to shift an unsigned int right one bit:

Dim myVar As UInt32 = UInt32.Parse("123456")
Dim myResult As UInt32
myResult = myVar >> 1

However, the >> operator only works on Byte, Short, Integer, and Long.

I also tried to convert to binary and manually shift the bits, but I only
found ways to go from binary to string--not back to binary.

Does anyone know how this could be done? (I'm not too concerned about
performance, so even resource-killing string manipulation would be fine with me.)


The BitConverter can "convert" from signed to unsigned integers.

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.ToInt32(BitConverter.GetBytes(u), 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function

David
Nov 20 '05 #3

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ur**************@TK2MSFTNGP11.phx.gbl...

"Marty McFly" <Ma***@Mc.Fly> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
Hello VB Gurus,

I have an unusual requirement to shift an unsigned int right one bit:

Dim myVar As UInt32 = UInt32.Parse("123456")
Dim myResult As UInt32
myResult = myVar >> 1

However, the >> operator only works on Byte, Short, Integer, and Long.

I also tried to convert to binary and manually shift the bits, but I only found ways to go from binary to string--not back to binary.

Does anyone know how this could be done? (I'm not too concerned about
performance, so even resource-killing string manipulation would be fine

with
me.)


The BitConverter can "convert" from signed to unsigned integers.

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.ToInt32(BitConverter.GetBytes(u), 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function


Oops, that screws up if the sign bit is set since >> shifts in another sign
bit.

This works, though

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitConverter.GetBytes(u), b, 4)
Dim s As Int64 = BitConverter.ToInt64(b, 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function
David
Nov 20 '05 #4

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
The BitConverter can "convert" from signed to unsigned integers.

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.ToInt32(BitConverter.GetBytes(u), 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function

David


There is an issue with the above if the high bit of the UInt32 is set, which
being a UInt, this could be likely.
If you perform the conversion as listed, things can get out of whack.
For example, let's say the initial UInt value is &h FFFF FFFF (d 4,294,967,295)
If you bit shift that 1 right, you get &h 7FFF FFFF (d 2,147,483,647)

In your sub, due to two's complement confusion, would return &h FFFF FFFF

Although this sub should work just fine for everything else between 0 and +7FFF
FFFF

Gerald
Nov 20 '05 #5
Oops, that screws up if the sign bit is set since >> shifts in another sign
bit.

This works, though

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitConverter.GetBytes(u), b, 4)
Dim s As Int64 = BitConverter.ToInt64(b, 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function
David


Heh, you caught it before I could get my Outlook to send.
How about this:

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim tmp As Int64 = Convert.ToInt64(u)
Return (Convert.ToUInt32(tmp >> 1))
End Function

:-)
Gerald
Nov 20 '05 #6
Just learning how to handle the shift operators and don't quite understand what you mean by "shifts in another sign bit". I use the >> and << operators on Postiive integers but never realized that shifting another negative integer results in the highest bit being reset to one..is this what heppans?
--
Dennis in Houston
"David Browne" wrote:

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ur**************@TK2MSFTNGP11.phx.gbl...

"Marty McFly" <Ma***@Mc.Fly> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
Hello VB Gurus,

I have an unusual requirement to shift an unsigned int right one bit:

Dim myVar As UInt32 = UInt32.Parse("123456")
Dim myResult As UInt32
myResult = myVar >> 1

However, the >> operator only works on Byte, Short, Integer, and Long.

I also tried to convert to binary and manually shift the bits, but I only found ways to go from binary to string--not back to binary.

Does anyone know how this could be done? (I'm not too concerned about
performance, so even resource-killing string manipulation would be fine

with
me.)


The BitConverter can "convert" from signed to unsigned integers.

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.ToInt32(BitConverter.GetBytes(u), 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function


Oops, that screws up if the sign bit is set since >> shifts in another sign
bit.

This works, though

Function RightShiftOneBit(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitConverter.GetBytes(u), b, 4)
Dim s As Int64 = BitConverter.ToInt64(b, 0)
Return BitConverter.ToUInt32(BitConverter.GetBytes(s >> 1), 0)
End Function
David

Nov 20 '05 #7

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
Just learning how to handle the shift operators and don't quite understand what you mean by "shifts in another sign bit". I use the >> and <<
operators on Postiive integers but never realized that shifting another
negative integer results in the highest bit being reset to one..is this what
heppans? --
Dennis in Houston


Try evaluating -1 >> 1. If a 0 were shifted into the high bit, then the
result would be a large positive integer (2^31 -1). If a 1 is shifted in
the result would be -1.

In fact the result is -1.

David
Nov 20 '05 #8

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

Similar topics

388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
5
by: LinuxGuy | last post by:
Hi, I want to add bits to present number. Shift operator pushes bits and add '0' at the end. I want to shift bits and want to add 1 at end Ex, 1)
4
by: HARDCORECODER | last post by:
ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
4
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) ...
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
4
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
3
by: Cindy | last post by:
I am struggling over a simple way to shift multi bytes for certain bits. Hope someone can help. For example, I open a memory space for 10 bytes: unsigned char *pData = new unsigned char; then...
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
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: 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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.