Skip to main content
Rollback to Revision 2
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 114

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    mask = mask * 2
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function
Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitexplicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


  • Edit 1: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop
  • Edit 2: re-introduce mask update which got lost in my first edit

edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    mask = mask * 2
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicit check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


  • Edit 1: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop
  • Edit 2: re-introduce mask update which got lost in my first edit

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

Fix error introduced by first edit
Source Link
Wolf
  • 375
  • 5
  • 18

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function
Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    mask = mask * 2
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitlyexplicit check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

  • Edit 1: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop
  • Edit 2: re-introduce mask update which got lost in my first edit

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    mask = mask * 2
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicit check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


  • Edit 1: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop
  • Edit 2: re-introduce mask update which got lost in my first edit
Tweeted twitter.com/StackCodeReview/status/1621252119725498368
removed error handling, see edit note
Source Link
Wolf
  • 375
  • 5
  • 18

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    On Error Resume Next
    mask = mask * 2
    If Err.Number <> 0 then
      Err.Clear
    End If
    On Error Goto 0
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
    On Error Resume Next
    mask = mask * 2
    If Err.Number <> 0 then
      Err.Clear
    End If
    On Error Goto 0
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm wondering if there was a simpler solution.

Function countBits(value)
  Dim n : n = 0
  Dim mask : mask = 1
  Dim i
  For i = 0 to 30
    If (value And mask) > 0 Then
      n = n +1
    End If
  Next
  If (value And &h8000) then
    n = n+1
  End If
  countBits = n
End Function

I found that there are no shift operators in VBScript and an overflow (Err.Number = 6) if I iterate for 0 to 31, that's why I add the explicitly check of the MSB after the look.

Any solutions to improve (maybe generalize) this?


edit: removed the useless error handling part, as it's a remains of starting with 31 as an upper bound in the first (and at that time only) loop

Source Link
Wolf
  • 375
  • 5
  • 18
Loading