Change Bit Value for Some positions

5 Ansichten (letzte 30 Tage)
Albert Sagala
Albert Sagala am 5 Dez. 2017
Kommentiert: Roger Stafford am 6 Dez. 2017
In binary number. Example 8 bit resolution in the measurement 10001111. In case I have many measurement data with 8 bits. And I want to change the bit value in the 4th bit only. If it is 0 then changes to 1, if it 1 then changes to 0.
so values 11101001 will become 11111001 and value 11100000 will become 11110000
I have a lof data, I can do changes in the 8th bits using command DEC2BIN(BIN2DEC(F4)+DEC2BIN(1)). I am still looking for the other positions.
Kind regards

Antworten (2)

Roger Stafford
Roger Stafford am 6 Dez. 2017
Use the 'xor' function. If one of the arguments is of type uint8 with a single bit equal to 1 and the rest 0, the corresponding bit in the other uint8 argument will be reversed.

Albert Sagala
Albert Sagala am 6 Dez. 2017
Bearbeitet: Albert Sagala am 6 Dez. 2017
xor(11110000,00010000)
ans =
0
I want the ans 11100000
  1 Kommentar
Roger Stafford
Roger Stafford am 6 Dez. 2017
My apologies! I should have said, "Use the bitxor function." (The 'xor' function handles logical trues and falses in a manner similar to & and | operations.) The 'bitxor' function deals with the individual bits in a number. However, writing bitxor(11110000,00010000) will undoubtedly interpret those as decimal, not binary, numbers.
Confession: At the moment I forget how one would enter binary numbers. In your example (out of desperation) I would write bitxor(15*16,16) where 15*16 in binary is 11110000 and 16 in binary is 00010000, which should give as a result 224, which has one bit reversed - that is, decimal 224 = binary 11100000.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Syntax for States and Transitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by