Das folgende Beispiel zeigt, wie in T-SQL, ein Bit in einem Smallint Datentype je Position geschaltet werden kann. Beispiel:
Pos.:F|E|D|C|B|A|9|8|7|6|5|4|3|2|1|0
Wert:0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|1 = 33 (Decimal)
Wert:0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1 = 32767 (Decimal)
[pastacode lang=“sql“ manual=“%2F*%0AT-SQL%20Bitwise%20operators%20to%20store%20multiple%20values%20in%20one%20number%20(bit%20flags)%0AI.Hill%2C%202015%2C%20mail%40werferstein.org%0A*%2F%0A%0A%0A%0ADECLARE%20%40Pos%20%20tinyint%20%20%3D%2013%09%09%09%09–Bit%20position%20to%20be%20read%20or%20written%09%0ADECLARE%20%40Flag%20bit%20%3D%200%09%09%09%09%09–Flag%20to%20be%20read%20or%20written%0A%0A%0A%0ADECLARE%20%40Set%20bit%20%3D%201%09%09%09%09%09–if%200%20The%20flag%20is%20read%20from%20the%20specified%20bit%20position%20of%20the%20value%0A%09%09%09%09%09%09%09%09%09%09–if%201%20The%20flag%20is%20stored%20at%20the%20specified%20bit%20position%20of%20the%20value%0ADECLARE%20%40optionValue%20smallint%20%20%3D%2032767%09–Value%20in%20which%20the%20bits%20are%20stored%20(only%2015%20bits%2C%20one%20bit%20is%20the%20sign%20bit%20–%3E%20pos.%2016)%0A%0A%0A%0A%0A——————————————————————————–%0A——————————————————————————–%0ADECLARE%20%40Expo%20%20smallint%20%3D%200%0ADECLARE%20%40MinusFlag%20smallint%20%3D%20-32768%0A%0A–In%20range%20%3F%0Aif%20(%40Pos%20%3E%200%20OR%20%40Pos%20%3C%2016)%20%20AND%20(%40optionValue%20%3E%3D%200%20OR%20%40optionValue%20%3C%3D%2032767)%0ABEGIN%09%0A%09%0A%09SET%20%40Expo%20%3D%20POWER%20(%202.0%2C%20%40Pos-1)%0A%0A%09IF%20%40Set%20%3D%200%0A%09–Value%20to%20flag%0A%09BEGIN%0A%09%09–Bit%20auf%20Pos.%20abfragen%0A%09%09SET%20%40Flag%20%3D%20%20%40optionValue%26%20%40Expo%20%0A%09END%0A%09ELSE%0A%09–Flag%20to%20value%0A%09BEGIN%0A%09%09–Turn%20off%20a%20bit%0A%09%09IF%20%40Flag%20%3D%200%20SET%20%20%40optionValue%20%26%3D%20~%40Expo%20%5E%20%40MinusFlag%20%0A%09%09–Turn%20on%20a%20bit%0A%09%09ELSE%20SET%20%40optionValue%20%7C%3D%20%40Expo%09%09%0A%09END%0AEND%0A%0A%0A————————————————————————————————%0A–Output%20of%20the%20values%20as%20a%20binary%0A————————————————————————————————%0ADECLARE%20%40WPOS%20INT%20%3D%2015%0ADECLARE%20%40BIN%20VARCHAR(15)%20%3D%20“%0AWHILE%20%40WPOS%20%3E%200%0ABegin%0A%09SET%20%40Expo%20%3D%20POWER%20(%202.0%2C%20%40WPOS-1)%20%0A%09%0A%09IF%20%40optionValue%20%26%20%40Expo%20%3E%200%20SET%20%40BIN%20%2B%3D%20’1’%0A%09ELSE%20SET%20%40BIN%20%2B%3D%20’0’%0ASET%20%40WPOS%20-%3D%201%0AEND%20%0A————————————————————————————————%0A————————————————————————————————%0APRINT%20’Pos%3A’%0APRINT%20%40Pos%0APRINT%20’Expo’%0APrint%20%40Expo%0APRINT%20’Flag’%0APrint%20%40Flag%0APRINT%20’Value’%0APRINT%20%40optionValue%0APRINT%20%40BIN%0A————————————————————————————————%0A————————————————————————————————%0A%0A%2F*%0A%26%3D%20F%C3%BChrt%20eine%20bitweise%20AND-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%09%0A%5E%3D%20F%C3%BChrt%20eine%20bitweise%20exklusive%20OR-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%0A%7C%3D%20F%C3%BChrt%20eine%20bitweise%20OR-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%20%0A*%2F%0A%0A%20″ message=“Bitweise Operatoren zum Speichern mehrerer Werte in einer Zahl (Bit-Flags)“ highlight=““ provider=“manual“/]