Bit Shifting in VBScript

# Thread: Bit Shifting in VBScript

1. Junior Member
Join Date
Dec 1969
Posts
5

## Bit Shifting in VBScript

I need to do a bit shift in a script, but can&#039;t seem to find any information on how to do it. Is this built into VBscript, or will I have to do it with logical operators?<BR>TIA

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Bit Shifting in VBScript

Dim x<BR>x = 2<BR>bitshiftRight = x * 2<BR>bitshiftLeft = x / 2<BR><BR>bout as close as you can get... however, I s&#039;pose you could start monkeying with<BR><BR>bit1 = x & 1<BR>bit2 = x & 2

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## I zigged when I shoulda zagged

I had it right in my mind, just didn&#039;t type it out :)<BR><BR>bitShiftLeft = x * 2<BR>bitShiftRight = x / 2<BR><BR>Tch.

4. Junior Member
Join Date
Dec 1969
Posts
5

## RE: I zigged when I shoulda zagged

Heh, thanks both of you.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## ** WRONG ANSWER **

Yes, <BR> bitShiftLeft = x * 2 &#039; okay<BR>** NO **<BR> bitShiftRight = x / 2 &#039; dead wrong<BR><BR>If you have a value in x of (say) 3, then doing <BR> x / 2<BR>will get you an answer of <BR> 1.5<BR>!!! You can fix that by then doing <BR> Int(x)<BR><BR>But VBScript *does* have an operator designed just for this need!<BR> bitShiftRight = x[hl="yellow"] [/hl]2<BR><BR>Yes, the *BACKSLASH* operator is an INTEGER DIVIDE in VBScript (and in VB and in VB.NET).<BR><BR>*CAUTION*<BR>You will *NOT* get correct results for all 32 bits using even integer divide. The best you can do is 31 bits. You can write functions that will account for all 32 bits, but they take more work than simply multiply and divide.<BR><BR><BR><BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## LOL... everytime I see a title

with your name and WRONG ANSWER in the recent posts, I always know it&#039;s associated with one of my answers... sigh......<BR><BR>Bitshifting in VBS is not such a good idea anyways... yeah.. that&#039;s my defense ;-)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•