-- 0 = Least Significant Bit, 31 = Most Significant Bit

local function rRotateSubsection(int, start, width, disp)
	local wrappedBits
	local shiftedBits

	disp = disp % width
	if disp == 0 then
		return int
	end

	wrappedBits = bit32.extract(int, start, disp)
	wrappedBits = bit32.lshift(wrappedBits, width - disp)
	shiftedBits = bit32.extract(int, start + disp, width - disp)

	return bit32.replace(int, bit32.bor(wrappedBits, shiftedBits), start, width)
end

function BitOps.rotateConnectivity(connectivity, degrees)
	return rRotateSubsection(connectivity, 0, 24, 6 * (degrees % 360) / 90)
end

function BitOps.rotateOccupancy(occupancy, degrees)
	local occupancyWithVerticalFacesRotated = rRotateSubsection(occupancy, 2, 4, (degrees % 360) / 90)
	return rRotateSubsection(occupancyWithVerticalFacesRotated, 6, 8, 2 * (degrees % 360) / 90)
end
