# byte manipulation..

#### Raph

##### New Member
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:

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..

### Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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.

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.

Replies
3
Views
643
Replies
2
Views
419
Replies
1
Views
2K
Replies
3
Views
594
Replies
0
Views
565

1,220,966
Messages
6,157,125
Members
451,399
Latest member
alchavar

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back