byte manipulation..

Raph

New Member
Joined
Jul 19, 2010
Messages
8
For example I want to invert the 1st, 3rd, 5th and 7th bit of this byte 01010101 the resulting byte must be 11111111 since the 1st, 3rd, 5th and 7th bit has been inverted..

I've already seen this code provided by yytsunamiyy but it seems like there is a problem with the code. Here is the code that he had worked on:

from (http://www.mrexcel.com/forum/showthread.php?t=482910)

Code:

Public Function INVERTBITS(Source As Range, bits_to_invert As String) As String
Dim i As Integer
Dim store As String, x as string

For i = 1 To Len(Source.Value)
x = i

If InStr(bits_to_invert, x) > 0 Then
If Mid(Source.Value, i, 1) = "0" Then
store = store & "1"
Else
store = store & "0"

End If
Else
store = store & Mid(Source.Value, i, 1)
On Error GoTo 0
End If

Next i
INVERTBITS = store

End Function


And here is how it should work:

Usage:

Source-Byte Result Formula in B
110 011 =INVERTBITS(A2;"13")
010 111 =INVERTBITS(A3;"13")
11111111 01010101 =INVERTBITS(A4;"1357")


I need all of your help, thanks.. :)
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Welcome to the Board. Your formulas worked for me:

<TABLE style="WIDTH: 215pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=286 x:str><COLGROUP><COL style="WIDTH: 48pt" span=2 width=64><COL style="WIDTH: 119pt; mso-width-source: userset; mso-width-alt: 5778" width=158><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 48pt; HEIGHT: 12.75pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22 height=17 width=64>110011</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; WIDTH: 48pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl23 width=64>011011</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; WIDTH: 119pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22 width=158>=INVERTBITS(A2,"13")</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22 height=17>010111</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl23>111111</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22>=INVERTBITS(A3,"13")</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22 height=17>01010101</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl23>11111111</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl22>=INVERTBITS(A4,"1357")</TD></TR></TBODY></TABLE>

Make sure that the cells in column A are formatted as Text before you enter the numbers. You can't format the cells as eg 00000000 and enter them, because any leading zeroes will be ignored.
 

Raph

New Member
Joined
Jul 19, 2010
Messages
8
Sir Andrew I've already determined the problem. Mr. yytsunamiyy used a semicolon instead of a comma (; instead of ,)
wrong = (A2;"13")
correct = (A2,"13")

Thank you for your time Sir and the tip about the format as text. Thanks. :)
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,939
Messages
5,508,243
Members
408,670
Latest member
lhmwnrexcel

This Week's Hot Topics

Top