RiftWorks
Найти
Персональное меню
Вы не представились системе
user-interface-preferences
Персональные инструменты
Обсуждение
Вклад
Создать учётную запись
Войти
Параметры
notifications
Редактирование:
Binary flags
(раздел)
associated-pages
Статья
Обсуждение
Просмотры
Читать
Править
Править код
История
Внимание:
Вы не вошли в систему. Ваш IP-адрес будет общедоступен, если вы запишете какие-либо изменения. Если вы
войдёте
или
создадите учётную запись
, её имя будет использоваться вместо IP-адреса, наряду с другими преимуществами.
Анти-спам проверка.
Не
заполняйте это!
=== Toggling bitflags === Sometimes, you want to toggle a bitflag, that is, if the bit on the flag is 1, you want to set it to 0, and if the bit is 0, you want to set it to 1 You can use the XOR function to do this due to the following properties * where the right hand side of the XOR is a 1, it will "toggle" the bits on the left hand side in the final output * where the right hand side of the XOR is a 0, it will leave the left hand side values unchanged in the final output If you refer back to the [[Binary_flags#Binary_Xor_.28_.5E_.29 | XOR truth table]] you can see these principles clearly First the truth table entries that deal with a 1 on the right hand side of the XOR operation * 0 ^ 1 = 1 * 1 ^ 1 = 0 Then the truth table entries that deal with a 0 on the right hand side of the XOR operation * 0 ^ 0 = 0 * 1 ^ 0 = 1 To help illustrate this concept, lets do an example where the user has the DISABILITY_ARM and the DISABILITY_EAR flags set and we want to toggle the DISABILITY_ARM flag to be the opposite of whatever it was before disabilities = disabilities ^ DISABILITY_ARM disabilities = 0b1010 ^ 0b1000 if we run through the XOR truth table for each pair of bits horizontally <pre> 0b1010 XOR 0b1000 RESULT 0b0010 </pre> Final result, the flag for DISABILITY_ARM is toggled off, but note that DISABILITY_EAR flag is unchanged, perfect! disabilities = 0b0010 If we want to toggle the flag again, we can repeat the operation on the current value of the binary flag variable disabilities = disabilities ^ DISABILITY_ARM disabilities = 0b0010 ^ 0b1000 (note DISABILITY_ARM is currently set to 0) <pre> 0b0010 XOR 0b1000 RESULT 0b1010 </pre> Final result, the DISABILITY_ARM flag is toggled back on, and DISABILITY_EAR remains unchanged still. disabilities = 0b1010 XOR can be trivially used then to toggle any specific flag by simply XORING the flag holding variable with the define that represents the bitflag you want to toggle, you can even combine sets of flags to toggle multiple at once e.g disabilities = disabilities ^ (DISABILITY_ARM | DISABILITY_LEG)
Описание изменений:
Обратите внимание, что все изменения в RiftWorks рассматриваются как выпущенные на условиях лицензии Creative Commons Attribution-NonCommercial-ShareAlike (см.
RiftWorks:Авторские права
). Если вы не хотите, чтобы ваши тексты свободно распространялись и редактировались любым желающим, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений или скопировали их из источника в общественном достоянии или под совместимой лицензией.
Не размещайте без разрешения материалы, защищённые авторским правом!
Отменить
Справка по редактированию
(в новом окне)