Skip to Content

Bitwise Operators

Overview

New in Vinyl 3.3, bitwise operators (also known as bit operators) are now supported. Bitwise operators are characters that represent actions (bitwise operations) that are performed on single bits. Depending on the database vendor, bitwise operations can be performed on int and byte columns and allows creating a column that holds multiple states of information.

The most common use case for bitwise operators is to use numeric values where the bits of the number are used to hold the bits of information:

  • 1: ...0001
  • 2: ...0010
  • 4: ...0100
  • ...

Bitwise operators can be used with any related options that are not exclusive (would not work on a dropdown list), and is commonly used for access/security (read/write access).

Supported Bitwise Operators

Operator Type Description Example
& And Returns only the bits both arguments have in common 1001 & 0011 = 0001
| Or Returns any bit that either argument has 1001 | 0011 = 1011
^ Exclusive Or Returns only bits that only one of the arguments have 1001 ^ 0011 = 1010
~ Negate Inverts all bits of a single argument ~1001 = 0110

Example Scenario

Bitwise operators can be used in a Schedule type scenario, where you want to display bit type indicators representing days of the week for different schedules. Here we would use logic to get the compound bit flags that represent Weekday and Weekend.

bitwiseexample.png

In this example scenario we assume there is a Day table, and will assign each bit to a day of the week:

DayID ID in bits Day (enum column)
1 0000001 Sunday
2 0000010 Monday
4 0000100 Tuesday
8 0001000 Wednesday
16 0010000 Thursday
32 0100000 Friday
64 1000000 Saturday

We can also have combined records:

DayID ID in bits Day (enum column)
62 0111110 Weekday
65 1000001 Weekend

Given this configuration in place, we could use this for a Schedule:

Schedule DayID DayID in bits
Runs on Mondays 2 0000010
Runs on Fridays 32 0100000
Runs on Tue and Wed 12 0001100

Note

Note that there would be no FK in this scenario, as not all possible combinations will be described in the Day table. In reality the Day table might not even be needed, but it helps with this scenario.

Now we can use the bitwise operators to either add, remove, or check the following:

  • Adds Friday to the schedule: UPDATE Schedule Set DayID = DayID | Enum(Day, 'Friday') WHERE ....
  • Toggles Friday from the schedule: UPDATE Schedule Set DayID = DayID ^ Enum(Day, 'Friday') WHERE ....
  • Checks if it runs on Fridays: IIF(DayID & Enum(Day, 'Friday') = Enum(Day, 'Friday'), 'Runs on Fridays', 'Don't run on Fridays')
  • Checks if it runs on the whole weekend: IIF(DayID & Enum(Day, 'Weekend') = Enum(Day, 'Weekend'), 'Runs on the whole Weekend', 'Don't run on Saturday and Sunday')
  • Checks if it runs on the at least one day on weekend: IIF(DayID & Enum(Day, 'Weekend') > 0, 'Runs on a weekend day', 'Don't run on any weekend day')

Logic Examples

Depending on your scenario, the Schedule logic required may be simple or more complex. Continuing with our Schedule example, if all Day options represent a single flag, your logic can be simple. For example: S.DayId ^ D.DayId

If your are using compound bit flags (Weekday and Weekend) you may need more complex logic. For example: IIF(S.DayId & D.DayId > 0, S.DayId ^ (S.DayId & D.DayId), S.DayId | D.DayId)