OpenTTD Source: src/core/bitmath_func.hpp File Reference

OpenTTD Source 14.0-beta3

Data Structures | Macros | Functions
bitmath_func.hpp File Reference

Go to the source code of this file.

## Data Structures
struct | SetBitIterator< Tbitpos, Tbitset >
| Iterable ensemble of each set bit in a value. More...
struct | SetBitIterator< Tbitpos, Tbitset >::Iterator

## Macros
#define | SETBITS(x, y) ((x) |= (y))
| Sets several bits in a variable. More...
#define | CLRBITS(x, y) ((x) &= ~(y))
| Clears several bits in a variable. More...

## Functions
template<typename T >
constexpr static debug_inline uint | GB (const T x, const uint8_t s, const uint8_t n)
| Fetch n bits from x, started at bit s. More...
template<typename T , typename U >
constexpr T | SB (T &x, const uint8_t s, const uint8_t n, const U d)
| Set n bits in x starting at bit s to d. More...
template<typename T , typename U >
constexpr T | AB (T &x, const uint8_t s, const uint8_t n, const U i)
| Add i to n bits of x starting at bit s. More...
template<typename T >
constexpr debug_inline bool | HasBit (const T x, const uint8_t y)
| Checks if a bit in a value is set. More...
template<typename T >
constexpr T | SetBit (T &x, const uint8_t y)
| Set a bit in a variable. More...
template<typename T >
constexpr T | ClrBit (T &x, const uint8_t y)
| Clears a bit in a variable. More...
template<typename T >
constexpr T | ToggleBit (T &x, const uint8_t y)
| Toggles a bit in a variable. More...
template<typename T >
constexpr uint8_t | FindFirstBit (T x)
| Search the first set bit in a value. More...
template<typename T >
constexpr uint8_t | FindLastBit (T x)
| Search the last set bit in a value. More...
template<typename T >
constexpr T | KillFirstBit (T value)
| Clear the first bit in an integer. More...
template<typename T >
constexpr uint | CountBits (T value)
| Counts the number of set bits in a variable. More...
template<typename T >
constexpr bool | HasExactlyOneBit (T value)
| Test whether value has exactly 1 bit set. More...
template<typename T >
constexpr bool | HasAtMostOneBit (T value)
| Test whether value has at most 1 bit set. More...
static uint32_t | BSWAP32 (uint32_t x)
| Perform a 32 bits endianness bitswap on x. More...
static uint16_t | BSWAP16 (uint16_t x)
| Perform a 16 bits endianness bitswap on x. More...

## Detailed Description

Functions related to bit mathematics.

Definition in file bitmath_func.hpp.

## Macro Definition Documentation

## ◆ CLRBITS

#define CLRBITS | ( |  | x,
|  |  | y
| ) |  | ((x) &= ~(y))

Clears several bits in a variable.

This macro clears several bits in a variable. The bits to clear are provided by a value. The new value is also returned.

Parameters

  x | The variable to clear some bits
  y | The value with set bits for clearing them in the variable

Returns
  The new value of x

Definition at line 166 of file bitmath_func.hpp.

## ◆ SETBITS

#define SETBITS | ( |  | x,
|  |  | y
| ) |  | ((x) |= (y))

Sets several bits in a variable.

This macro sets several bits in a variable. The bits to set are provided by a value. The new value is also returned.

Parameters

  x | The variable to set some bits
  y | The value with set bits for setting them in the variable

Returns
  The new value of x

Definition at line 136 of file bitmath_func.hpp.

## Function Documentation

## ◆ AB()

template<typename T , typename U >

constexpr T AB | ( | T & | x,
|  | const uint8_t | s,
|  | const uint8_t | n,
|  | const U | i
| ) |  |
| constexpr

Add i to n bits of x starting at bit s.

This adds the value of i on n bits of x starting at bit s. The parameters x, s, i are similar to GB. Besides, \ a x must be a variable as the result are saved there. An overflow does not affect the following bits of the given bit window and is simply ignored.

Note
  Parameter x must be a variable as the result is saved there.

Parameters

  x | The variable to add some bits at some position
  s | The start position of the addition
  n | The size/window for the addition

Precondition
  n < sizeof(T) * 8
  s + n <= sizeof(T) * 8

Parameters

  i | The value to add at the given start position in the given window.

Returns
  The new value of x

Definition at line 83 of file bitmath_func.hpp.

Referenced by IncHouseConstructionTick(), and IncreaseRoadWorksCounter().

## ◆ BSWAP16()

static uint16_t BSWAP16 | ( | uint16_t | x | ) |
| inlinestatic

Perform a 16 bits endianness bitswap on x.

Parameters

  x | the variable to bitswap

Returns
  the bitswapped value.

Definition at line 360 of file bitmath_func.hpp.

## ◆ BSWAP32()

static uint32_t BSWAP32 | ( | uint32_t | x | ) |
| inlinestatic

Perform a 32 bits endianness bitswap on x.

Parameters

  x | the variable to bitswap

Returns
  the bitswapped value.

Definition at line 345 of file bitmath_func.hpp.

Referenced by AddGrfInfo(), NewGRFInspectWindow::DrawMainPanelWidget(), NewGRFProfiler::GetOutputFilename(), GraphicsSetLoadConfig(), GraphicsSetSaveConfig(), HandleNode(), HandleSavegameLoadCrash(), HasGRFConfig(), IsGoodGRFConfigList(), LoadNewGRFSound(), ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(), ShowMissingContentWindow(), and SurveyGrfs().

## ◆ ClrBit()

template<typename T >

constexpr T ClrBit | ( | T & | x,
|  | const uint8_t | y
| ) |  |
| constexpr

Clears a bit in a variable.

This function clears a bit in a variable. The variable is changed and the value is also returned. Parameter y defines the bit to clear and starts at the LSB with 0.

Parameters

  x | The variable to clear the bit
  y | The bit position to clear

Precondition
  y < sizeof(T) * 8

Returns
  The new value of the old value with the bit cleared

Definition at line 151 of file bitmath_func.hpp.

Referenced by AircraftHandleDestTooFar(), RoadStop::AllocateBay(), RoadStop::AllocateDriveThroughBay(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearArticulatedPart(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearEngine(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearFreeWagon(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearFrontEngine(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearMultiheaded(), ClearSingleBridgeMiddle(), ClearSnow(), GroundVehicle< RoadVehicle, VEH_ROAD >::ClearWagon(), CmdAutofillTimetable(), CopyGRFConfigList(), DisableEngineForCompany(), Vehicle::HandlePathfindingResult(), RoadStop::MakeDriveThrough(), MapSpriteMappingRecolour(), PrepareUnload(), ReadSpriteLayout(), RemoveFirstTrack(), RemoveFirstTrackdir(), SetGroupFlag(), StationHandleBigTick(), ToggleInvisibilityWithTransparency(), TryPathReserve(), UpdateTownGrowth(), GroundVehicle< RoadVehicle, VEH_ROAD >::UpdateZPositionAndInclination(), and VehicleEnterDepot().

## ◆ CountBits()

template<typename T >

constexpr uint CountBits | ( | T | value | ) |
| constexpr

Counts the number of set bits in a variable.

Parameters

  value | the value to count the number of bits in.

Returns
  the number of bits.

Definition at line 243 of file bitmath_func.hpp.

Referenced by AfterLoadCompanyStats(), CalculateCompanyAssetValue(), CmdConvertRail(), DrawRoadTypeCatenary(), RailTypeScopeResolver::GetRandomBits(), and RoadTypeScopeResolver::GetRandomBits().

## ◆ FindFirstBit()

template<typename T >

constexpr uint8_t FindFirstBit | ( | T | x | ) |
| constexpr

Search the first set bit in a value.

When no bit is set, it returns 0.

Parameters

  x | The value to search.

Returns
  The position of the first bit set.

Definition at line 194 of file bitmath_func.hpp.

Referenced by CYapfBaseT< Types >::AddMultipleNodes(), FindFirstTrack(), FindFirstTrackdir(), CYapfOriginTileT< Types >::PfSetStartupNodes(), RemoveFirstTrack(), RemoveFirstTrackdir(), TrackBitsToTrack(), and YapfRoadVehicleChooseTrack().

## ◆ FindLastBit()

template<typename T >

constexpr uint8_t FindLastBit | ( | T | x | ) |
| constexpr

Search the last set bit in a value.

When no bit is set, it returns 0.

Parameters

  x | The value to search.

Returns
  The position of the last bit set.

Definition at line 213 of file bitmath_func.hpp.

## ◆ GB()

template<typename T >

constexpr static debug_inline uint GB | ( | const T | x,
|  | const uint8_t | s,
|  | const uint8_t | n
| ) |  |
| staticconstexpr

Fetch n bits from x, started at bit s.

This function can be used to fetch n bits from the value x. The s value set the start position to read. The start position is count from the LSB and starts at `0`. The result starts at a LSB, as this isn't just an and-bitmask but also some bit-shifting operations. GB(0xFF, 2, 1) will so return 0x01 (0000 0001) instead of 0x04 (0000 0100).

Parameters

  x | The value to read some bits.
  s | The start position to read some bits.
  n | The number of bits to read.

Precondition
  n < sizeof(T) * 8
  s + n <= sizeof(T) * 8

Returns
  The selected bits, aligned to a LSB.

Definition at line 32 of file bitmath_func.hpp.

Referenced by Buffer::AppendUtf8(), AreCompanyManagerFaceBitsValid(), BuildObject(), ChangeTrainDirRandomly(), CheckPartialPixelZ(), CmdBuildObjectArea(), CmdClearArea(), CmdLevelLand(), ConvertFromOldCompanyManagerFace(), DecodeMonitorCargoType(), DecodeMonitorCompany(), Disaster_CoalMine_Init(), DisasterTick_Submarine(), DrawCommonTileSeq(), DrawSpriteToRgbaBuffer(), AIInfo::DummyConstructor(), GetBridgeAxis(), GetBridgeType(), GetCleanHouseType(), GetCleanIndustryGfx(), GetClearCounter(), GetClearDensity(), StoryPageButtonData::GetColour(), GetCompanyManagerFaceBits(), GetCompanyManagerFaceSprite(), Order::GetConditionComparator(), Order::GetConditionVariable(), GetCrossingRoadAxis(), StoryPageButtonData::GetCursor(), GetCustomRoadStopSpecIndex(), Order::GetDepotActionType(), Order::GetDepotOrderType(), GetDisallowedRoadDirections(), GetDistanceFromNearbyHouse(), GetFeatureIndex(), GetFeatureNum(), GetFieldType(), GetGlobalVariable(), GetHouseConstructionTick(), GetHouseProcessingTime(), GetHouseTriggers(), GetIndustryConstructionCounter(), GetIndustryTriggers(), GetLiftDestination(), GetLiftPosition(), Order::GetLoadType(), GetLockDirection(), GetLockPart(), Waypoint::GetNewGRFVariable(), Order::GetNonStopType(), GetPresentSignals(), GetRailDepotDirection(), GetRailReservationTrackBits(), GetRailType(), RailTypeScopeResolver::GetRandomBits(), RoadTypeScopeResolver::GetRandomBits(), GetRawClearGround(), GetRoadDepotDirection(), GetRoadside(), VehicleResolverObject::GetScope(), GetShipDepotAxis(), GetShipDepotPart(), GetSignalStates(), GetStationTileRandomBits(), GetStationType(), Order::GetStopLocation(), GetTileOwner(), GetTrackBits(), GetTreeCount(), GetTreeDensity(), GetTreeGround(), GetTreeGrowth(), GetTropicZone(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), Order::GetType(), Order::GetUnloadType(), GRFParameterInfo::GetValue(), GetVarFileType(), IndustriesScopeResolver::GetVariable(), TownScopeResolver::GetVariable(), VehicleScopeResolver::GetVariable(), GetVarMemType(), StoryPageButtonData::GetVehicleType(), GetWaterClass(), GetWaterTileType(), HandleAutoSignalPlacement(), HandleCrashedAircraft(), RoadStop::HasFreeBay(), IncHouseConstructionTick(), IncreaseRoadWorksCounter(), IsBridgeAbove(), IsCustomRoadStopSpecIndex(), MirrorRoadBits(), MoveWaypointsToBaseStations(), TimetableWindow::OnInvalidateData(), OrdersWindow::OnInvalidateData(), Order::Order(), PlaceTree(), Packet::PrepareToSend(), ResetRestoreAllTransparency(), River_Hash(), RotateRoadBits(), ScaleAllCompanyManagerFaceBits(), SeedChance(), Packet::Send_uint32(), Packet::Send_uint64(), SetHouseType(), SetIndustryGfx(), SpriteFontCache::SetUnicodeGlyph(), VehicleListIdentifier::UnpackIfValid(), Utf8Decode(), Utf8EncodedCharLen(), StoryPageButtonData::ValidateColour(), StoryPageButtonData::ValidateCursor(), and StoryPageButtonData::ValidateVehicleType().

## ◆ HasAtMostOneBit()

template<typename T >

constexpr bool HasAtMostOneBit | ( | T | value | ) |
| constexpr

Test whether value has at most 1 bit set.

Parameters

  value | the value to test.

Returns
  does value have at most 1 bit set?

Definition at line 271 of file bitmath_func.hpp.

Referenced by GetNewCargoTypeForReplace(), MaskWireBits(), and Vehicle::NeedsServicing().

## ◆ HasBit()

template<typename T >

constexpr debug_inline bool HasBit | ( | const T | x,
|  | const uint8_t | y
| ) |  |
| constexpr

Checks if a bit in a value is set.

This function checks if a bit inside a value is set or not. The y value specific the position of the bit, started at the LSB and count from `0`.

Parameters

  x | The value to check
  y | The position of the bit to check, started from the LSB

Precondition
  y < sizeof(T) * 8

Returns
  True if the bit is set, false else.

Definition at line 103 of file bitmath_func.hpp.

Referenced by AddArticulatedParts(), NewGRFWindow::AddGRFToActive(), LinkGraph::AddNode(), AfterLoadRoadStops(), AircraftHandleDestTooFar(), RoadStop::AllocateBay(), AnimationBase< IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback, TileAnimationFrameAnimationHelper< Industry > >::AnimateTile(), TimetableWindow::BuildArrivalDepartureList(), BaseVehicleListWindow::BuildCargoDropDownList(), BuildObject(), RefitWindow::BuildRefitList(), CompanyStationsWindow::BuildStationsList(), CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::CanEnterNewTile(), CanEnterTile(), CanStationTileHavePylons(), CanStationTileHaveWires(), CargoAndEngineFilter(), ChangeIndustryProduction(), CheckAutoreplaceValidity(), CheckBuildableTile(), CheckIfIndustryTilesAreFree(), RoadStop::Entry::CheckIntegrity(), BuildRailStationWindow::CheckSelectedSize(), ChooseShipTrack(), CmdAlterGroup(), CmdAutoreplaceVehicle(), CmdBuyCompany(), CmdForceTrainProceed(), CmdReverseTrainDirection(), CmdSetVehicleOnTime(), CmdStartStopVehicle(), ConvertFromOldCompanyManagerFace(), ConvertOldMultiheadToNew(), BaseConsist::CopyConsistPropertiesFrom(), CountArticulatedParts(), Train::Crash(), DisasterTick_Helicopter_Rotors(), DisasterTick_Submarine(), DisasterTick_Ufo(), DisasterTick_Zeppeliner(), TownAuthorityWindow::DrawActions(), TimetableWindow::DrawArrivalDeparturePanel(), DrawBridgeMiddle(), DrawCommonTileSeq(), IndustryViewWindow::DrawInfo(), TownAuthorityWindow::DrawRatings(), EngineReplacement(), FillNewGRFVehicleCache(), FindGRFConfig(), FindTrainOnTrackEnum(), Train::GetAccelerationStatus(), GetAcceptanceMask(), GetBestFittingSubType(), GetCanalSpriteOffset(), GetCargoSubtypeText(), GetCargoSuffix(), GetCompanyRailTypes(), GetCompanyRoadTypes(), RoadStop::GetEntry(), GetFoundation_Town(), GetIfStopIsForType(), GetIncompatibleRefitOrderIdForAutoreplace(), GetIndustryProbabilityCallback(), Airport::GetNumHangars(), GetRailReservationTrackBits(), GetRailStationAxis(), GetRailTypes(), GetRoadTypes(), GetSingleSignalState(), GetTLG(), CompanyInfrastructureWindow::GetTotalMaintenanceCost(), BaseGraphWindow::GetValuesInterval(), StationScopeResolver::GetVariable(), GetWaterTileType(), GRFLoadConfig(), GroundSpritePaletteTransform(), HandleBankruptcyTakeover(), Vehicle::HandlePathfindingResult(), LinkRefresher::HandleRefit(), HandleSavegameLoadCrash(), HasCPUIDFlag(), HasCrossingReservation(), HasDepotReservation(), HasGrfMiscBit(), HasPowerOnRail(), HasPowerOnRoad(), HasRailCatenary(), HasRailTypeAvail(), GoodsEntry::HasRating(), HasRoadCatenary(), HasStationReservation(), HasTrack(), HasTrackdir(), HasTunnelBridgeReservation(), HasTunnelBridgeSnowOrDesert(), IndustryTemporarilyRefusesCargo(), IsArticulatedEngine(), Vehicle::IsArticulatedPart(), IsArticulatedVehicleRefittable(), IsBridge(), IsCompatibleRail(), IsCrossingBarred(), NWidgetCore::IsDisabled(), IsDockingTile(), Engine::IsEnabled(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsEngine(), IsEngineBuildable(), IsEngineRefittable(), RoadStop::IsEntranceBusy(), ObjectSpec::IsEverAvailable(), RoadStop::IsFreeBay(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsFreeWagon(), IsGoodGRFConfigList(), Engine::IsHidden(), NWidgetCore::IsHighlighted(), IsHouseCompleted(), IsIndustryCompleted(), IsInvisibilitySet(), IsLoggableGrfConfig(), NWidgetCore::IsLowered(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsMultiheaded(), IsOnSnow(), IsRoadStopAvailable(), IsSignalPresent(), IsSnowTile(), IsStationAvailable(), IsStationTileBlocked(), IsTransparencySet(), IsTunnel(), IsValidCompanyManagerFace(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsWagon(), RoadStop::Leave(), LiftHasDestination(), MapSpriteMappingRecolour(), MarkTrainAsStuck(), MonitorMonitorsIndustry(), MoveWaypointsToBaseStations(), Vehicle::NeedsServicing(), NetworkAfterNewGRFScan(), NetworkAutoCleanCompanies(), NetworkCompanyIsPassworded(), NewVehicleAvailable(), NPFGetFlag(), TownAuthorityWindow::OnInvalidateData(), NewGRFWindow::OnInvalidateData(), VehicleListWindow::OnInvalidateData(), TownAuthorityWindow::OnPaint(), SelectCompanyManagerFaceWindow::OnPaint(), SignList::OwnerVisibilityFilter(), ParamSet(), PerformStationTileSlopeCheck(), PopupMainCompanyToolbMenu(), LinkRefresher::PredictNextOrder(), PrepareUnload(), Rail90DegTurnDisallowed(), RailNoLevelCrossings(), ReadSpriteLayout(), RoadStop::Entry::Rebuild(), RefitVehicle(), RoadNoLevelCrossing(), ScaleAllCompanyManagerFaceBits(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), BuildVehicleWindow::SetCargoFilterArray(), SetYearEngineAgingStops(), ShowDropDownMenu(), ShowMissingContentWindow(), ShowNewGrfVehicleError(), Vehicle::ShowVisualEffect(), SlReadSimpleGamma(), SpriteLayoutPaletteTransform(), SubtractMoneyFromAnyCompany(), SurveyGrfs(), TileLoop_Water(), TownDirectoryWindow::TownRatingSorter(), TownTickHandler(), TrackOverlapsTracks(), TrainController(), TriggerObjectAnimation(), TriggerObjectTileAnimation(), TriggerRoadStopRandomisation(), TriggerWatchedCargoCallbacks(), TryPathReserve(), OrdersWindow::UpdateAutoRefitState(), UpdateCompanyGroupLiveries(), Train::UpdateDeltaXY(), UpdateObjectColours(), OskWindow::UpdateOskState(), UpdateTownGrowthRate(), Vehicle::UpdateVisualEffect(), GroundVehicle< RoadVehicle, VEH_ROAD >::UpdateZPosition(), Utf8Decode(), VehicleExitDir(), VehicleLengthChanged(), VerifyAutoreplaceRefitForOrders(), and RoadStop::~RoadStop().

## ◆ HasExactlyOneBit()

template<typename T >

constexpr bool HasExactlyOneBit | ( | T | value | ) |
| constexpr

Test whether value has exactly 1 bit set.

Parameters

  value | the value to test.

Returns
  does value have exactly 1 bit set?

Definition at line 259 of file bitmath_func.hpp.

## ◆ KillFirstBit()

template<typename T >

constexpr T KillFirstBit | ( | T | value | ) |
| constexpr

Clear the first bit in an integer.

This function returns a value where the first bit (from LSB) is cleared. So, 110100 returns 110000, 000001 returns 000000, etc.

Parameters

  value | The value to clear the first bit

Returns
  The new value with the first bit cleared

Definition at line 231 of file bitmath_func.hpp.

Referenced by CYapfBaseT< Types >::AddMultipleNodes(), ExtendTrainReservation(), CYapfOriginTileT< Types >::PfSetStartupNodes(), TrackBitsToTrack(), and TracksOverlap().

## ◆ SB()

template<typename T , typename U >

constexpr T SB | ( | T & | x,
|  | const uint8_t | s,
|  | const uint8_t | n,
|  | const U | d
| ) |  |
| constexpr

Set n bits in x starting at bit s to d.

This function sets n bits from x which started as bit s to the value of d. The parameters x, s and n works the same as the parameters of GB. The result is saved in x again. Unused bits in the window provided by n are set to 0 if the value of d isn't "big" enough. This is not a bug, its a feature.

Note
  Parameter x must be a variable as the result is saved there.
  To avoid unexpected results the value of d should not use more space as the provided space of n bits (log2)

Parameters

  x | The variable to change some bits
  s | The start position for the new bits
  n | The size/window for the new bits
  d | The actually new bits to save in the defined position.

Precondition
  n < sizeof(T) * 8
  s + n <= sizeof(T) * 8

Returns
  The new value of x

Definition at line 58 of file bitmath_func.hpp.

Referenced by ClearEnginesHiddenFlagOfCompany(), CmdSetVehicleVisibility(), EncodeCargoIndustryMonitor(), EncodeCargoTownMonitor(), RoadStop::Enter(), HaltLift(), MakeBridgeRamp(), MakeField(), MakeLockTile(), MakeObject(), MakeRailDepot(), MakeRailTunnel(), MakeRoadCrossing(), MakeRoadDepot(), MakeRoadNormal(), MakeRoadTunnel(), MakeShipDepot(), MakeShore(), MakeStation(), MakeTree(), MakeWater(), MoveBuoysToWaypoints(), NetworkServerUpdateCompanyPassworded(), NPFSetFlag(), ResetIndustryConstructionStage(), VehicleOrderSaver::Restore(), SetClearCounter(), SetClearDensity(), SetCompanyManagerFaceBits(), Order::SetConditionComparator(), Order::SetConditionVariable(), SetCrossingBarred(), SetCrossingReservation(), SetCustomRoadStopSpecIndex(), Order::SetDepotActionType(), Order::SetDepotOrderType(), SetDepotReservation(), SetDisallowedRoadDirections(), SetDockingTile(), RoadStop::SetEntranceBusy(), SetFieldType(), SetHasSignals(), SetHouseCompleted(), SetHouseProcessingTime(), SetHouseTriggers(), SetHouseType(), SetIndustryCompleted(), SetIndustryConstructionCounter(), SetIndustryConstructionStage(), SetIndustryGfx(), SetIndustryTriggers(), SetLiftDestination(), SetLiftPosition(), Order::SetLoadType(), Order::SetNonStopType(), SetPresentSignals(), SetRailDepotExitDirection(), SetRailStationReservation(), SetRailType(), SetRoadDepotExitDirection(), SetRoadside(), SetSignalStates(), SetStationTileBlocked(), SetStationTileHavePylons(), SetStationTileHaveWires(), SetStationTileRandomBits(), Order::SetStopLocation(), GRFConfig::SetSuitablePalette(), SetTileOwner(), SetTileType(), SetTrackBits(), SetTrackReservation(), SetTreeGroundDensity(), SetTreeGrowth(), SetTropicZone(), SetTunnelBridgeReservation(), SetTunnelBridgeSnowOrDesert(), Order::SetUnloadType(), GRFParameterInfo::SetValue(), StoryPageButtonData::SetVehicleType(), SetWaterClass(), TerminateRoadWorks(), and UpdateStationAcceptance().

## ◆ SetBit()

template<typename T >

constexpr T SetBit | ( | T & | x,
|  | const uint8_t | y
| ) |  |
| constexpr

Set a bit in a variable.

This function sets a bit in a variable. The variable is changed and the value is also returned. Parameter y defines the bit and starts at the LSB with 0.

Parameters

  x | The variable to set a bit
  y | The bit position to set

Precondition
  y < sizeof(T) * 8

Returns
  The new value of the old value with the bit set

Definition at line 121 of file bitmath_func.hpp.

Referenced by AircraftHandleDestTooFar(), CalculateRefitMasks(), CalculateRoadStopCost(), ChangeTownRating(), CmdAutofillTimetable(), ConvertFromOldCompanyManagerFace(), ConvertOldMultiheadToNew(), BaseConsist::CopyConsistPropertiesFrom(), CopyGRFConfigList(), DrawCommonTileSeq(), EnableEngineForCompany(), EncodeCargoIndustryMonitor(), RoadStop::Enter(), RoadStop::FreeBay(), GetAcceptanceMask(), GetAvailableVehicleCargoTypes(), GetCompanyRailTypes(), GetCompanyRoadTypes(), GetCountAndDistanceOfClosestInstance(), GetEmptyMask(), GetMaskOfAllowedCurrencies(), Airport::GetNumHangars(), GetRailTypes(), GetRoadTypes(), GRFLoadConfig(), GRFUnsafe(), HandleBankruptcyTakeover(), Vehicle::HandlePathfindingResult(), InitializeSortedCargoSpecs(), IsEngineRefittable(), IsGoodGRFConfigList(), LookupManyOfMany(), RoadStop::MakeDriveThrough(), MakeSnow(), MapSpriteMappingRecolour(), MarkTrainAsStuck(), NetworkAutoCleanCompanies(), GraphLegendWindow::OnInvalidateData(), PrepareUnload(), ReadGRFSpriteOffsets(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetArticulatedPart(), SetBridgeMiddle(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetEngine(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetFreeWagon(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetFrontEngine(), SetGroupFlag(), SetLiftDestination(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetMultiheaded(), SmallMapWindow::SetOverlayCargoMask(), SettingsDisableElrail(), GroundVehicle< RoadVehicle, VEH_ROAD >::SetWagon(), ShowNewGrfVehicleError(), ToggleInvisibilityWithTransparency(), TriggerRoadStopRandomisation(), TriggerWatchedCargoCallbacks(), and GroundVehicle< RoadVehicle, VEH_ROAD >::UpdateZPositionAndInclination().

## ◆ ToggleBit()

template<typename T >

constexpr T ToggleBit | ( | T & | x,
|  | const uint8_t | y
| ) |  |
| constexpr

Toggles a bit in a variable.

This function toggles a bit in a variable. The variable is changed and the value is also returned. Parameter y defines the bit to toggle and starts at the LSB with 0.

Parameters

  x | The variable to toggle the bit
  y | The bit position to toggle

Precondition
  y < sizeof(T) * 8

Returns
  The new value of the old value with the bit toggled

Definition at line 181 of file bitmath_func.hpp.

Referenced by BaseConsist::CopyConsistPropertiesFrom(), ToggleInvisibility(), ToggleSnow(), ToggleTransparency(), and ToggleTransparencyLock().