|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Want to know the power-2 based numbers of xto VB? Or give an example of how to perform this operation? $x = 9124 ; $n = 1 ; while ( $x > 0 ) { if ( $x & 1 == 1 ) { echo $n, "\n" ; } $n *= 2 ; $x >>= 1 ; } // Will output... // 4 // 32 // 128 // 256 // 512 // 8192 Chad Miller wrote:
> The only example I was able to find was written in PHP. Can anyone An exact translation would be as follows:> convert to VB? Or give an example of how to perform this operation? \\\ Dim x As Integer Dim n As Integer x = 9124 n = 1 Do While x > 0 If (x And 1) = 1 Then Debug.Print(CStr(n)) End If n *= 2 x \= 2 Loop /// This provides the same output as your PHP code. HTH, -- (O)enone Chad Miller wrote:
Show quoteHide quote > The only example I was able to find was written in PHP. Can anyone convert This is basically just converting to binary and then listing the bits> to VB? Or give an example of how to perform this operation? > > $x = 9124 ; > > $n = 1 ; > while ( $x > 0 ) { > if ( $x & 1 == 1 ) { > echo $n, "\n" ; > } > $n *= 2 ; > $x >>= 1 ; > } > > // Will output... > // 4 > // 32 > // 128 > // 256 > // 512 > // 8192 that are set. the following code works, it may not be the best way to do things, however: Dim value As Integer = 9124 Dim bit As Integer = 0 While (2 ^ bit) <= value Dim power As Integer = CInt(2 ^ bit) If (value And power) = power Then Console.WriteLine(power.ToString) End If bit += 1 End While
Querying a database using vb
Foreign letters in text file OpenGL Refresh problem using CsGL with VB.NET Resolving Type mismatch, HOW How to disable the UAC in Vista? Deriving Combobox items indexes bypass security in Outlook How to keep statusStrip at the bottom of the ToolStripContainer Sub(Thing as Object) {thing = new Thing.GetType} ? GPS and VB .net 2005 |
|||||||||||||||||||||||