binary math in 2005

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
I need to do a simple routine that a processor would handle easily. is there a way to incorporate such a feat in VB.NET. say i want to mirror image the number 73. this is a task done easily on a processor i.e.

75 in binary is 01001011 (8 bits) the mirror is 11010010 (210)

i transfer each bit into a registry one by one

(Shift L) ACC (Shift R)
S 01001011 > 0 > 00000000

1 00100101 > 1 > 00000001
2 00010010 > 1 > 00000011
3 00001001 > 0 > 00000110
4 00000100 > 1 > 00001101
5 00000010 > 0 > 00011010
6 00000001 > 0 > 00110100
7 00000000 > 1 > 01101001
8 00000000 > 0 > 11010010

F 00000000 > > 11010010 = 210


this would be very fast on a processor, how can i do this quickly in VB.NET?

i want to alter the method i have wrote which essentially converts the decimal number into a string then performs a series of substring functions on it while creating a new string with the inverted binary number finally converting this string back to a number which is displayed along with the binary. Yes it works but it takes a considerable number of processor calls to achieve one of the simplest tasks a processor can handle.

i would be prepared to perform this in any language including machine code if i can integrate it into the 2005 IDE. any advice would be appreciated as this conversion is a bottleneck in my application.
 
Your 75/210 numbers is swapped.. Use the BitArray class for this. You also need the BitConverter class to get things to and from byte array.
 
in C# i did this by:

VB.NET:
    static int revd(int i)
    {
      int o = (i & 1) << 7 | ((i & 2) << 5) | ((i & 4) << 3) | ((i & 8) << 1) | ((i & 16) >> 1) | ((i & 32) >> 3) | ((i & 64 ) >> 5) | ((i & 128) >> 7);
      return o;
    }

it works like:

mask off the bit you want with bitwise AND
slide it into place with bitshifting
all bits are orred together for result

I have no knowledge of bitwise operations in VBN
 
just what i needed johnH nice to know you can tweak individual bits of code in VB.NET some of this will come in useful with another application i'm working on which has a bank of 16 flags that react with each other, this will tidy the spaggeti in that code .

i will look into your method too cjard it seems very close to assembly language
 

Latest posts

Back
Top