473,698 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("1 23456")
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 2905
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("12 3456")
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******** ******@TK2MSFTN GP09.phx.gbl...
Hello VB Gurus,

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

Dim myVar As UInt32 = UInt32.Parse("1 23456")
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******** ******@TK2MSFTN GP09.phx.gbl...
Hello VB Gurus,

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

Dim myVar As UInt32 = UInt32.Parse("1 23456")
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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.To Int32(BitConver ter.GetBytes(u) , 0)
Return BitConverter.To UInt32(BitConve rter.GetBytes(s >> 1), 0)
End Function

David
Nov 20 '05 #3

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:ur******** ******@TK2MSFTN GP11.phx.gbl...

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

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

Dim myVar As UInt32 = UInt32.Parse("1 23456")
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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.To Int32(BitConver ter.GetBytes(u) , 0)
Return BitConverter.To UInt32(BitConve rter.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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitC onverter.GetByt es(u), b, 4)
Dim s As Int64 = BitConverter.To Int64(b, 0)
Return BitConverter.To UInt32(BitConve rter.GetBytes(s >> 1), 0)
End Function
David
Nov 20 '05 #4

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in message
news:ur******** ******@TK2MSFTN GP11.phx.gbl...
The BitConverter can "convert" from signed to unsigned integers.

Function RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.To Int32(BitConver ter.GetBytes(u) , 0)
Return BitConverter.To UInt32(BitConve rter.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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitC onverter.GetByt es(u), b, 4)
Dim s As Int64 = BitConverter.To Int64(b, 0)
Return BitConverter.To UInt32(BitConve rter.GetBytes(s >> 1), 0)
End Function
David


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

Function RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim tmp As Int64 = Convert.ToInt64 (u)
Return (Convert.ToUInt 32(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" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:ur******** ******@TK2MSFTN GP11.phx.gbl...

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

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

Dim myVar As UInt32 = UInt32.Parse("1 23456")
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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim s As Int32 = BitConverter.To Int32(BitConver ter.GetBytes(u) , 0)
Return BitConverter.To UInt32(BitConve rter.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 RightShiftOneBi t(ByVal u As UInt32) As UInt32
Dim b(8) As Byte
Array.Copy(BitC onverter.GetByt es(u), b, 4)
Dim s As Int64 = BitConverter.To Int64(b, 0)
Return BitConverter.To UInt32(BitConve rter.GetBytes(s >> 1), 0)
End Function
David

Nov 20 '05 #7

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:1B******** *************** ***********@mic rosoft.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
21699
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
43
26501
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
12787
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
3100
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 variable. here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 or
12
2194
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 the most significant bit in a given element in an array of unsigned longs. Now I realise there may be a more efficient way to do this, and if you know a better way please let me know.
4
4283
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) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
24
3174
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 wrong about it. No where google helps me determining this lapse in my memory. MSVC 6 compiler gives me error. Thanks && Regards,
4
4187
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
3133
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 store some value into pData which occupies pData and pData. Now I want to shift these two bytes to the left for 9 bits and still store them in pData. The data now should span from the bit0 in pData to bit1 in pData.
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.