(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
authortron
Wed, 20 Jul 2005 15:29:28 +0000
changeset 2140 a04d0142ad65
parent 2139 63d965b6e229
child 2141 d3167783c73c
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
ai_old.c
aircraft_cmd.c
clear_cmd.c
disaster_cmd.c
engine.c
industry_cmd.c
landscape.c
music_gui.c
network_data.c
oldloader.c
order.h
order_cmd.c
players.c
rail_cmd.c
road_cmd.c
settings_gui.c
station_cmd.c
strings.c
town_cmd.c
train_cmd.c
tunnelbridge_cmd.c
vehicle.c
win32.c
--- a/ai_old.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/ai_old.c	Wed Jul 20 15:29:28 2005 +0000
@@ -1534,15 +1534,16 @@
 static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 {
 	uint values[NUM_CARGO];
-	int w,h;
 	int rad;
 
 	for(;p->mode != 4;p++) if (p->mode == 1) {
 		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
-
-		w = ((p->attr>>1) & 7);
-		h = ((p->attr>>4) & 7);
-		if (p->attr&1) intswap(w, h);
+		uint w;
+		uint h;
+
+		w = GB(p->attr, 1, 3);
+		h = GB(p->attr, 4, 3);
+		if (p->attr & 1) uintswap(w, h);
 
 
 		if (_patches.modified_catchment) {
--- a/aircraft_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/aircraft_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -1045,13 +1045,13 @@
 
 	if (v->u.air.crashed_counter < 650) {
 		if (CHANCE16R(1,32,r)) {
-			v->direction = (v->direction+_crashed_aircraft_moddir[(r >> 16)&3]) & 7;
+			v->direction = (v->direction + _crashed_aircraft_moddir[GB(r, 16, 2)]) & 7;
 			SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
 			r = Random();
 			CreateEffectVehicleRel(v,
-				4 + (r&0xF),
-				4 + ((r>>4)&0xF),
-				((r>>8)&0xF),
+				GB(r, 0, 4) + 4,
+				GB(r, 4, 4) + 4,
+				GB(r, 8, 4),
 				EV_EXPLOSION_SMALL);
 		}
 	} else if (v->u.air.crashed_counter >= 10000) {
--- a/clear_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/clear_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -470,7 +470,7 @@
 	if (ti->tileh != 0) {
 		DrawGroundSprite(0xFA0 + _tileh_to_sprite[ti->tileh]);
 	} else {
-		DrawGroundSprite(_landscape_clear_sprites[((ti->x^ti->y) >> 4) & 0x7]);
+		DrawGroundSprite(_landscape_clear_sprites[GB(ti->x ^ ti->y, 4, 3)]);
 	}
 }
 
@@ -485,11 +485,11 @@
 	}
 
 	if (img & 0x38) {
-		DrawGroundSpriteAt(_clear_land_fence_sprites_1[((img >> 3) & 7) - 1] + _fence_mod_by_tileh[ti->tileh], ti->x, ti->y, z);
+		DrawGroundSpriteAt(_clear_land_fence_sprites_1[GB(img, 3, 3) - 1] + _fence_mod_by_tileh[ti->tileh], ti->x, ti->y, z);
 	}
 
 	if (img & 0x7) {
-		DrawGroundSpriteAt(_clear_land_fence_sprites_1[(img & 7) - 1] + _fence_mod_by_tileh_2[ti->tileh], ti->x, ti->y, z);
+		DrawGroundSpriteAt(_clear_land_fence_sprites_1[GB(img, 0, 3) - 1] + _fence_mod_by_tileh_2[ti->tileh], ti->x, ti->y, z);
 	}
 }
 
@@ -785,7 +785,7 @@
 		r = Random();
 		tile = RandomTileSeed(r);
 		if (IsTileType(tile, MP_CLEAR)) {
-			j = ((r >> 16) & 0xF) + 5;
+			j = GB(r, 16, 4) + 5;
 			for(;;) {
 				TileIndex tile_new;
 
--- a/disaster_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/disaster_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -226,9 +226,9 @@
 			uint32 r = Random();
 
 			CreateEffectVehicleRel(v,
-				-7 + (r&0xF),
-				-7 + (r>>4&0xF),
-				5 + (r>>8&0x7),
+				GB(r, 0, 4) - 7,
+				GB(r, 4, 4) - 7,
+				GB(r, 8, 3) + 5,
 				EV_EXPLOSION_SMALL);
 		}
 	} else if (v->age == 350) {
@@ -364,9 +364,9 @@
 			uint32 r = Random();
 
 			CreateEffectVehicleAbove(
-				x + (r & 0x3F),
-				y + (r >> 6 & 0x3F),
-				(r >> 12 & 0xF),
+				GB(r,  0, 6) + x,
+				GB(r,  6, 6) + y,
+				GB(r, 12, 4),
 				EV_EXPLOSION_SMALL);
 
 			if (++v->age >= 55)
@@ -435,9 +435,9 @@
 			uint32 r = Random();
 
 			CreateEffectVehicleAbove(
-				x + (r & 0x3F),
-				y + (r >> 6 & 0x3F),
-				(r >> 12 & 0xF),
+				GB(r,  0, 6) + x,
+				GB(r,  6, 6) + y,
+				GB(r, 12, 4),
 				EV_EXPLOSION_SMALL);
 
 			if (++v->age >= 55)
@@ -620,8 +620,8 @@
 		for(i=0; i!=80; i++) {
 			uint32 r = Random();
 			CreateEffectVehicleAbove(
-				v->x_pos-32+(r&0x3F),
-				v->y_pos-32+(r>>5&0x3F),
+				GB(r, 0, 6) + v->x_pos - 32,
+				GB(r, 5, 6) + v->y_pos - 32,
 				0,
 				EV_EXPLOSION_SMALL);
 		}
--- a/engine.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/engine.c	Wed Jul 20 15:29:28 2005 +0000
@@ -196,22 +196,22 @@
 		e->player_avail = 0;
 
 		r = Random();
-		e->intro_date = (uint16)((r & 0x1FF) + ei->base_intro);
+		e->intro_date = GB(r, 0, 9) + ei->base_intro;
 		if (e->intro_date <= _date) {
 			e->age = (_date - e->intro_date) >> 5;
 			e->player_avail = (byte)-1;
 			e->flags |= ENGINE_AVAILABLE;
 		}
 
-		e->reliability_start = (uint16)(((r >> 16) & 0x3fff) + 0x7AE0);
+		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
 		r = Random();
-		e->reliability_max = (uint16)((r & 0x3fff) + 0xbfff);
-		e->reliability_final = (uint16)(((r>>16) & 0x3fff) + 0x3fff);
+		e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
+		e->reliability_final = GB(r, 16, 14) + 0x3FFF;
 
 		r = Random();
-		e->duration_phase_1 = (uint16)((r & 0x1F) + 7);
-		e->duration_phase_2 = (uint16)(((r >> 5) & 0xF) + ei->base_life * 12 - 96);
-		e->duration_phase_3 = (uint16)(((r >> 9) & 0x7F) + 120);
+		e->duration_phase_1 = GB(r, 0, 5) + 7;
+		e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
+		e->duration_phase_3 = GB(r, 9, 7) + 120;
 
 		e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
 
--- a/industry_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/industry_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -315,7 +315,7 @@
 {
 	int image = _m[ti->tile].owner;
 	if (image & 0x80) {
-		image = (image >> 2) & 0x1F;
+		image = GB(image, 2, 5);
 		if (image != 0 && image < 7) {
 			AddChildSpriteScreen(image + 0x806,
 				_coal_plant_sparkles_x[image-1],
@@ -1802,7 +1802,7 @@
 	int j;
 	Industry *i;
 
-	type = _new_industry_rand[_opt.landscape][(r >> 16) & 0x1F];
+	type = _new_industry_rand[_opt.landscape][GB(r, 16, 5)];
 	if (type == IT_OIL_WELL && _date > 10958)
 		return;
 
--- a/landscape.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/landscape.c	Wed Jul 20 15:29:28 2005 +0000
@@ -352,7 +352,7 @@
 
 	va_start(va, flags);
 
-	if ((i = (flags >> 8) & 0xF) != 0) {
+	if ((i = GB(flags, 8, 4)) != 0) {
 		SetTileType(tile, i - 1);
 	}
 
@@ -485,7 +485,7 @@
 	if (x < 2 || y < 2)
 		return;
 
-	direction = (r >> 22) & 3;
+	direction = GB(r, 22, 2);
 	if (direction & 1) {
 		w = template->height;
 		h = template->width;
@@ -632,16 +632,16 @@
 			GenerateTerrain(2, 0);
 
 		r = Random();
-		flag = (r & 3) | 4;
-		for (i = ScaleByMapSize(((r >> 16) & 0x7F) + 450); i != 0; --i)
+		flag = GB(r, 0, 2) | 4;
+		for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i)
 			GenerateTerrain(4, flag);
 	} else if (_opt.landscape == LT_DESERT) {
 		for (i = ScaleByMapSize((Random()&0x7F) + 170); i != 0; --i)
 			GenerateTerrain(0, 0);
 
 		r = Random();
-		flag = (r & 3) | 4;
-		for (i = ScaleByMapSize(((r >> 16) & 0xFF) + 1700); i != 0; --i)
+		flag = GB(r, 0, 2) | 4;
+		for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i)
 			GenerateTerrain(0, flag);
 
 		flag ^= 2;
--- a/music_gui.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/music_gui.c	Wed Jul 20 15:29:28 2005 +0000
@@ -132,8 +132,8 @@
 		i = 500;
 		do {
 			uint32 r = InteractiveRandom();
-			byte *a = &_cur_playlist[r & 0x1F];
-			byte *b = &_cur_playlist[(r >> 8)&0x1F];
+			byte *a = &_cur_playlist[GB(r, 0, 5)];
+			byte *b = &_cur_playlist[GB(r, 8, 5)];
 
 			if (*a != 0 && *b != 0) {
 				byte t = *a;
--- a/network_data.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/network_data.c	Wed Jul 20 15:29:28 2005 +0000
@@ -44,30 +44,30 @@
 void NetworkSend_uint16(Packet *packet, uint16 data)
 {
 	assert(packet->size < sizeof(packet->buffer) - sizeof(data));
-	packet->buffer[packet->size++] = data & 0xFF;
-	packet->buffer[packet->size++] = (data >> 8) & 0xFF;
+	packet->buffer[packet->size++] = GB(data, 0, 8);
+	packet->buffer[packet->size++] = GB(data, 8, 8);
 }
 
 void NetworkSend_uint32(Packet *packet, uint32 data)
 {
 	assert(packet->size < sizeof(packet->buffer) - sizeof(data));
-	packet->buffer[packet->size++] = data & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >> 8) & 0xFF;
+	packet->buffer[packet->size++] = GB(data,  0, 8);
+	packet->buffer[packet->size++] = GB(data,  8, 8);
+	packet->buffer[packet->size++] = GB(data, 16, 8);
+	packet->buffer[packet->size++] = GB(data, 24, 8);
 }
 
 void NetworkSend_uint64(Packet *packet, uint64 data)
 {
 	assert(packet->size < sizeof(packet->buffer) - sizeof(data));
-	packet->buffer[packet->size++] = data & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >>= 8) & 0xFF;
-	packet->buffer[packet->size++] = (data >> 8) & 0xFF;
+	packet->buffer[packet->size++] = GB(data,  0, 8);
+	packet->buffer[packet->size++] = GB(data,  8, 8);
+	packet->buffer[packet->size++] = GB(data, 16, 8);
+	packet->buffer[packet->size++] = GB(data, 24, 8);
+	packet->buffer[packet->size++] = GB(data, 32, 8);
+	packet->buffer[packet->size++] = GB(data, 40, 8);
+	packet->buffer[packet->size++] = GB(data, 48, 8);
+	packet->buffer[packet->size++] = GB(data, 56, 8);
 }
 
 // Sends a string over the network. It sends out
--- a/oldloader.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/oldloader.c	Wed Jul 20 15:29:28 2005 +0000
@@ -722,8 +722,8 @@
 	if (st->xy != 0) {
 		if (st->train_tile) {
 			/* Calculate the trainst_w and trainst_h */
-			int w = (_old_platforms >> 3) & 0x7;
-			int h = (_old_platforms & 0x7);
+			uint w = GB(_old_platforms, 3, 3);
+			uint h = GB(_old_platforms, 0, 3);
 			st->trainst_w = w;
 			st->trainst_h = h;
 		}
--- a/order.h	Wed Jul 20 09:49:04 2005 +0000
+++ b/order.h	Wed Jul 20 15:29:28 2005 +0000
@@ -135,9 +135,9 @@
 static inline Order UnpackOrder(uint32 packed)
 {
 	Order order;
-	order.type    = (packed & 0x000000FF);
-	order.flags   = (packed & 0x0000FF00) >> 8;
-	order.station = (packed & 0xFFFF0000) >> 16;
+	order.type    = GB(packed,  0,  8);
+	order.flags   = GB(packed,  8,  8);
+	order.station = GB(packed, 16, 16);
 	order.next    = NULL;
 	order.index   = 0; // avoid compiler warning
 	return order;
--- a/order_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/order_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -40,9 +40,9 @@
 Order UnpackOldOrder(uint16 packed)
 {
 	Order order;
-	order.type    = (packed & 0x000F);
-	order.flags   = (packed & 0x00F0) >> 4;
-	order.station = (packed & 0xFF00) >> 8;
+	order.type    = GB(packed, 0, 4);
+	order.flags   = GB(packed, 4, 4);
+	order.station = GB(packed, 8, 8);
 	order.next    = NULL;
 
 	// Sanity check
@@ -63,9 +63,9 @@
 Order UnpackVersion4Order(uint16 packed)
 {
 	Order order;
-	order.type    = (packed & 0x000F);
-	order.flags   = (packed & 0x00F0) >> 4;
-	order.station = (packed & 0xFF00) >> 8;
+	order.type    = GB(packed, 0, 4);
+	order.flags   = GB(packed, 4, 4);
+	order.station = GB(packed, 8, 8);
 	order.next    = NULL;
 	order.index   = 0; // avoid compiler warning
 	return order;
@@ -144,17 +144,17 @@
 /** Add an order to the orderlist of a vehicle.
  * @param x,y unused
  * @param p1 various bitstuffed elements
- * - p1 = (bit  0 - 15) - ID of the vehicle (p1 & 0xFFFF)
+ * - p1 = (bit  0 - 15) - ID of the vehicle
  * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
- *                        the order will be inserted before that one (p1 & 0xFFFF0000)>>16
- *                        only the first 8 bytes used currently (bit 16 - 23) (max 255)
+ *                        the order will be inserted before that one
+ *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
  * @param p2 packed order to insert
  */
 int32 CmdInsertOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
-	VehicleID veh = p1 & 0xFFFF;
-	OrderID sel_ord = p1 >> 16;
+	VehicleID veh   = GB(p1,  0, 16);
+	OrderID sel_ord = GB(p1, 16, 16);
 	Order new_order = UnpackOrder(p2);
 
 	if (!IsVehicleIndex(veh)) return CMD_ERROR;
@@ -509,18 +509,18 @@
 /** Modify an order in the orderlist of a vehicle.
  * @param x,y unused
  * @param p1 various bitstuffed elements
- * - p1 = (bit  0 - 15) - ID of the vehicle (p1 & 0xFFFF)
+ * - p1 = (bit  0 - 15) - ID of the vehicle
  * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
- *                        the order will be inserted before that one (p1 & 0xFFFF0000)>>16
- *                        only the first 8 bytes used currently (bit 16 - 23) (max 255)
+ *                        the order will be inserted before that one
+ *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
  * @param p2 mode to change the order to (always set)
  */
 int32 CmdModifyOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
 	Order *order;
-	OrderID sel_ord = p1 >> 16; // XXX - automatically truncated to 8 bits.
-	VehicleID veh = p1 & 0xFFFF;
+	OrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits.
+	VehicleID veh   = GB(p1,  0, 16);
 
 	if (!IsVehicleIndex(veh)) return CMD_ERROR;
 	if (p2 != OFB_FULL_LOAD && p2 != OFB_UNLOAD && p2 != OFB_NON_STOP && p2 != OFB_TRANSFER) return CMD_ERROR;
@@ -583,8 +583,8 @@
 int32 CmdCloneOrder(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *dst;
-	VehicleID veh_src = (p1 >> 16) & 0xFFFF;
-	VehicleID veh_dst = p1 & 0xFFFF;
+	VehicleID veh_src = GB(p1, 16, 16);
+	VehicleID veh_dst = GB(p1,  0, 16);
 
 	if (!IsVehicleIndex(veh_dst)) return CMD_ERROR;
 
--- a/players.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/players.c	Wed Jul 20 15:29:28 2005 +0000
@@ -45,7 +45,7 @@
 	/* draw the chin */
 	/* FIXME: real code uses -2 in zoomlevel 1 */
 	{
-		uint val = (face >> 4) & 3;
+		uint val = GB(face, 4, 2);
 		if (!(flag & 2)) {
 			DrawSprite(0x327 + (flag&1?0:val), x, y);
 		} else {
@@ -54,8 +54,8 @@
 	}
 	/* draw the eyes */
 	{
-		uint val1 = (face >> 6)&15;
-		uint val2 = (face >> 20)&7;
+		uint val1 = GB(face,  6, 4);
+		uint val2 = GB(face, 20, 3);
 		uint32 high = 0x314<<16;
 
 		if (val2 >= 6) {
@@ -81,7 +81,7 @@
 
 	/* draw the mouth */
 	{
-		uint val = (face >> 10) & 63;
+		uint val = GB(face, 10, 6);
 		uint val2;
 
 		if (!(flag&1)) {
@@ -127,7 +127,7 @@
 
 	/* draw the hair */
 	{
-		uint val = (face >> 16) & 15;
+		uint val = GB(face, 16, 4);
 		if (!(flag&2)) {
 			if (!(flag&1)) {
 				DrawSprite(0x382 + (val*9>>4), x, y);
@@ -145,7 +145,7 @@
 
 	/* draw the tie */
 	{
-		uint val = (face >> 20) & 0xFF;
+		uint val = GB(face, 20, 8);
 
 		if (!(flag&1)) {
 			DrawSprite(0x36B + ((val&3)*3>>2), x, y);
@@ -164,7 +164,7 @@
 
 	/* draw the glasses */
 	{
-		uint val = (face >> 28) & 7;
+		uint val = GB(face, 28, 3);
 
 		if (!(flag&2)) {
 			if (val<=1) {
@@ -371,7 +371,7 @@
 	n = 100;
 	do {
 		r = Random();
-		COLOR_SWAP(r & 0xF, (r >> 4) & 0xF);
+		COLOR_SWAP(GB(r, 0, 4), GB(r, 4, 4));
 	} while (--n);
 
 	// Bubble sort it according to the values in table 1
@@ -754,8 +754,8 @@
 	} break;
 
 	case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */
-		PlayerID pid_old = p2 & 0xFFFF;
-		PlayerID pid_new = (p2 >> 16) & 0xFFFF;
+		PlayerID pid_old = GB(p2,  0, 16);
+		PlayerID pid_new = GB(p2, 16, 16);
 
 		if (pid_old >= MAX_PLAYERS || pid_new >= MAX_PLAYERS) return CMD_ERROR;
 
--- a/rail_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/rail_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -577,7 +577,7 @@
 {
 	int ex, ey;
 	int32 ret, total_cost = 0;
-	Track track = (Track)((p2 >> 4) & 7);
+	Track track = (Track)GB(p2, 4, 3);
 	Trackdir trackdir;
 	byte mode = HASBIT(p2, 7);
 
@@ -845,7 +845,7 @@
 	bool error = true;
 
 	int mode = p2 & 0x1;
-	Track track = (p2 >> 4) & 7;
+	Track track = GB(p2, 4, 3);
 	Trackdir trackdir = TrackToTrackdir(track);
 	byte semaphores = (HASBIT(p2, 3)) ? 8 : 0;
 	byte signal_density = (p2 >> 24);
--- a/road_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/road_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -789,7 +789,7 @@
 			image = _road_tile_sprites_1[ti->map5 & 0xF];
 		}
 
-		m2 = (_m[ti->tile].m4 & 0x70) >> 4;
+		m2 = GB(_m[ti->tile].m4, 4, 3);
 
 		if (m2 == 0) image |= 0x3178000;
 
@@ -835,7 +835,7 @@
 		if ( _m[ti->tile].m4 & 0x80) {
 			image += 8;
 		} else {
-			m2 = (_m[ti->tile].m4 & 0x70) >> 4;
+			m2 = GB(_m[ti->tile].m4, 4, 3);
 			if (m2 == 0) image |= 0x3178000;
 			if (m2 > 1) image += 4;
 		}
@@ -1006,7 +1006,7 @@
 	if (_m[tile].m5 & 0xE0)
 		return;
 
-	if (((_m[tile].m4 & 0x70) >> 4) < 6) {
+	if (GB(_m[tile].m4, 4, 3) < 6) {
 		t = ClosestTownFromTile(tile, (uint)-1);
 
 		grp = 0;
@@ -1018,7 +1018,7 @@
 					!(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) &&
 					(_m[tile].m5==5 || _m[tile].m5==10)) {
 				if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1,20)) {
-					_m[tile].m4 |= ((((_m[tile].m4 & 0x70) >> 4 ) <=  2) ? 7 : 6) << 4;
+					_m[tile].m4 |= (GB(_m[tile].m4, 4, 3) <=  2 ? 7 : 6) << 4;
 
 					SndPlayTileFx(SND_21_JACKHAMMER, tile);
 					CreateEffectVehicleAbove(
@@ -1034,7 +1034,7 @@
 
 		{
 			const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp];
-			byte b = (_m[tile].m4 & 0x70) >> 4;
+			byte b = GB(_m[tile].m4, 4, 3);
 
 			if (b == p[0])
 				return;
@@ -1062,7 +1062,7 @@
 			return;
 		}
 		//roadworks finished
-		_m[tile].m4 = ((((b& 0x70) >> 4)== 6) ? 1 : 2) << 4;
+		_m[tile].m4 = (GB(b, 4, 3) == 6 ? 1 : 2) << 4;
 		MarkTileDirtyByTile(tile);
 	}
 }
@@ -1090,7 +1090,7 @@
 		byte b = _m[tile].m5;
 		if ((b & 0xF0) == 0) {
 			/* Ordinary road */
-			if (!_road_special_gettrackstatus && ((_m[tile].m4&0x70) >> 4) >= 6)
+			if (!_road_special_gettrackstatus && GB(_m[tile].m4, 4, 3) >= 6)
 				return 0;
 			return _road_trackbits[b&0xF] * 0x101;
 		} else if (IsLevelCrossing(tile)) {
@@ -1125,7 +1125,7 @@
 {
 	int i = (_m[tile].m5 >> 4);
 	if (i == 0)
-		i = ((_m[tile].m4 & 0x70) >> 4) + 3;
+		i = GB(_m[tile].m4, 4, 3) + 3;
 	td->str = _road_tile_strings[i - 1];
 	td->owner = GetTileOwner(tile);
 }
--- a/settings_gui.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/settings_gui.c	Wed Jul 20 15:29:28 2005 +0000
@@ -1019,16 +1019,16 @@
 
 /** Network-safe changing of patch-settings.
  * @param p1 various bitstuffed elements
- * - p1 = (bit 0- 7) - the patches type (page) that is being changed (construction, network, ai) (p1 & 0xFF)
- * - p2 = (bit 8-15) - the actual patch (entry) being set inside the category ((p1>>8) & 0xFF)
+ * - p1 = (bit 0- 7) - the patches type (page) that is being changed (construction, network, ai)
+ * - p2 = (bit 8-15) - the actual patch (entry) being set inside the category
  * @param p2 the new value for the patch
  * @todo check that the new value is a valid one. Awful lot of work, but since only
  * the server is allowed to do this, we trust it on this one :)
  */
 int32 CmdChangePatchSetting(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	byte pcat = p1 & 0xFF;
-	byte pel = (p1 >> 8) & 0xFF;
+	byte pcat = GB(p1, 0, 8);
+	byte pel  = GB(p1, 8, 8);
 
 	if (pcat >= lengthof(_patches_page)) return CMD_ERROR;
 	if (pel >= _patches_page[pcat].num) return CMD_ERROR;
--- a/station_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/station_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -933,8 +933,8 @@
  * @param x,y starting position of station dragging/placement
  * @param p1 various bitstuffed elements
  * - p1 = (bit  0)    - orientation (p1 & 1)
- * - p1 = (bit  8-15) - number of tracks (p1 >> 8) & 0xFF)
- * - p1 = (bit 16-23) - platform length (p1 >> 16) & 0xFF)
+ * - p1 = (bit  8-15) - number of tracks
+ * - p1 = (bit 16-23) - platform length
  * @param p2 various bitstuffed elements
  * - p2 = (bit  0- 3) - railtype (p2 & 0xF)
  * - p2 = (bit  4)    - set for custom station (p2 & 0x10)
@@ -961,8 +961,8 @@
 
 	/* unpack parameters */
 	direction = p1 & 1;
-	numtracks = (p1 >> 8) & 0xFF;
-	plat_len = (p1 >> 16) & 0xFF;
+	numtracks = GB(p1,  8, 8);
+	plat_len  = GB(p1, 16, 8);
 	/* w = length, h = num_tracks */
 	if (direction) {
 		h_org = plat_len;
--- a/strings.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/strings.c	Wed Jul 20 15:29:28 2005 +0000
@@ -179,8 +179,8 @@
 // should set those bits.
 char *GetStringWithArgs(char *buffr, uint string, const int32 *argv)
 {
-	uint index = string & 0x7FF;
-	uint tab = (string >> 11) & 0x1F;
+	uint index = GB(string,  0, 11);
+	uint tab   = GB(string, 11,  5);
 
 	if (!(string & 0xFFFF)) {
 		error("!invalid string id 0 in GetString");
--- a/town_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/town_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -769,9 +769,10 @@
 static int GenRandomRoadBits(void)
 {
 	uint32 r = Random();
-	int a = r&3, b = (r >> 8) & 3;
+	uint a = GB(r, 0, 2);
+	uint b = GB(r, 8, 2);
 	if (a == b) b ^= 2;
-	return (1<<a)+(1<<b);
+	return (1 << a) + (1 << b);
 }
 
 // Grow the town
@@ -1307,7 +1308,7 @@
 				ChangePopulation(t, _housetype_population[house]);
 
 			// Initial value for map5.
-			m5 = (r >> 16) & 0x3F;
+			m5 = GB(r, 16, 6);
 		}
 
 		assert(IsTileType(tile, MP_CLEAR));
--- a/train_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/train_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -3013,9 +3013,9 @@
 				r = Random();
 
 				CreateEffectVehicleRel(u,
-					2 + ((r>>8)&7),
-					2 + ((r>>16)&7),
-					5 + (r&7),
+					GB(r,  8, 3) + 2,
+					GB(r, 16, 3) + 2,
+					GB(r,  0, 3) + 5,
 					EV_EXPLOSION_SMALL);
 				break;
 			}
--- a/tunnelbridge_cmd.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/tunnelbridge_cmd.c	Wed Jul 20 15:29:28 2005 +0000
@@ -184,8 +184,8 @@
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
 	/* unpack parameters */
-	bridge_type = p2 & 0xFF;
-	railtype = (byte)(p2 >> 8);
+	bridge_type = GB(p2, 0, 8);
+	railtype    = GB(p2, 8, 8);
 
 	if (p1 > MapSize()) return CMD_ERROR;
 
@@ -1023,8 +1023,8 @@
 		if (ice)
 			image += 32;
 
-		image += _draw_tunnel_table_1[(ti->map5 >> 2) & 0x3];
-		image += (ti->map5 & 3) << 1;
+		image += _draw_tunnel_table_1[GB(ti->map5, 2, 2)];
+		image += GB(ti->map5, 0, 2) * 2;
 		DrawGroundSprite(image);
 
 		AddSortableSpriteToDraw(image+1, ti->x + 15, ti->y + 15, 1, 1, 8, (byte)ti->z);
@@ -1055,7 +1055,7 @@
 			}
 
 			// bridge ending.
-			b = _bridge_sprite_table[(_m[ti->tile].m2 >> 4) & 0xF][6];
+			b = _bridge_sprite_table[GB(_m[ti->tile].m2, 4, 4)][6];
 			b += (tmp&(3<<1))*4; /* actually ((tmp>>2)&3)*8 */
 			b += (tmp&1); // direction
 			if (ti->tileh == 0) b += 4; // sloped "entrance" ?
@@ -1077,7 +1077,7 @@
 			uint z;
 			int x,y;
 
-			image = (ti->map5 >> 3) & 3;	// type of stuff under bridge (only defined for 0,1)
+			image = GB(ti->map5, 3, 2); // type of stuff under bridge (only defined for 0,1)
 			assert(image <= 1);
 
 			if (!(ti->map5 & 0x20)) {
@@ -1115,7 +1115,7 @@
 				DrawGroundSprite(image);
 			}
 			// get bridge sprites
-			b = _bridge_sprite_table[(_m[ti->tile].m2 >> 4) & 0xF][_m[ti->tile].m2&0xF] + tmp * 4;
+			b = _bridge_sprite_table[GB(_m[ti->tile].m2, 4, 4)][GB(_m[ti->tile].m2, 0, 4)] + tmp * 4;
 
 			z = GetBridgeHeight(ti) + 5;
 
@@ -1285,9 +1285,9 @@
 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
 {
 	if ((_m[tile].m5 & 0x80) == 0) {
-		td->str = STR_5017_RAILROAD_TUNNEL + ((_m[tile].m5 >> 2) & 3);
+		td->str = STR_5017_RAILROAD_TUNNEL + GB(_m[tile].m5, 2, 2);
 	} else {
-		td->str = _bridge_tile_str[ (_m[tile].m2 >> 4) + (((_m[tile].m5>>1)&3)<<4) ];
+		td->str = _bridge_tile_str[GB(_m[tile].m5, 1, 2) << 4 | GB(_m[tile].m2, 4, 4)];
 
 		/* scan to the end of the bridge, that's where the owner is stored */
 		if (_m[tile].m5 & 0x40) {
--- a/vehicle.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/vehicle.c	Wed Jul 20 15:29:28 2005 +0000
@@ -612,8 +612,8 @@
 static void ChimneySmokeInit(Vehicle *v)
 {
 	uint32 r = Random();
-	v->cur_image = SPR_CHIMNEY_SMOKE_0 + (r & 7);
-	v->progress = (r >> 16) & 7;
+	v->cur_image = SPR_CHIMNEY_SMOKE_0 + GB(r, 0, 3);
+	v->progress = GB(r, 16, 3);
 }
 
 static void ChimneySmokeTick(Vehicle *v)
@@ -1299,8 +1299,8 @@
 
 	/* check if to break down */
 	if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) {
-		v->breakdown_ctr = (byte)(((r >> 16) & 0x3F) + 0x3F);
-		v->breakdown_delay = (byte)(((r >> 24) & 0x7F) | 0x80);
+		v->breakdown_ctr    = GB(r, 16, 6) + 0x3F;
+		v->breakdown_delay  = GB(r, 24, 7) + 0x80;
 		v->breakdown_chance = 0;
 	}
 }
--- a/win32.c	Wed Jul 20 09:49:04 2005 +0000
+++ b/win32.c	Wed Jul 20 15:29:28 2005 +0000
@@ -2142,8 +2142,8 @@
 #if defined(_MSC_VER)
 	{
 		uint64 seed = rdtsc();
-		_random_seeds[0][0] = seed & 0xffffffff;
-		_random_seeds[0][1] = seed >> 32;
+		_random_seeds[0][0] = GB(seed,  0, 32);
+		_random_seeds[0][1] = GB(seed, 32, 32);
 	}
 #else
 	_random_seeds[0][0] = GetTickCount();