[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/vr/ - Retro Games

Search:


View post   

>> No.2871128 [View]
File: 4 KB, 370x400, spark_man_sprite__mm3__by_blue__bomber-d85r7zi.png [View same] [iqdb] [saucenao] [google]
2871128

>>2869148
Next up is the roll/shift operations. These are really easy to grasp.

First, the shifts. There are two types of shift operations, shift left and shift right. They do pretty much what you'd expect; they shift the bits in a number over by one bit in the specified direction, whilst moving in a 0 bit. For example, lets left shift on a 4-bit computer the number 5 = 0b0101:

ShiftLeft(0b0101) => 0b1010 = 10, carry = 0

One thing to notice about the above is that 10 is twice that of 5. This isn't a coincidence. Shifting left is effectively multiplication by 2. A similar thing happens in decimal; when you shift a number left you multiply by 10. In other words shifting left multiplies a number by the base you're shifting in. On a computer you always shift binary numbers, so the factor is always 2. As mentioned, a 0 is shifted into the first position. Furthermore the last bit that gets shifted out is placed into the Carry flag of the program status register (the carry flag gets used for lots of things).

Shift right works exactly like shift left, but the bits move right instead. Likewise this can be thought as division by 2. Furthermore the Carry flag can be interpreted as the remainder of the division.

ShiftRight(0b0101) => 0b0010 = 2, carry = 1

The rolls are very similar to the shifts. The only difference is that instead of always shifting in a 0, the contents of the carry flag gets shifted in. The bit that gets shifted out is still put into the carry. In effect the number that's being shifted and the carry form a circular buffer that can be rolled round and round. For example, let's set the carry to 0 and roll left the number 3 = 0b0011 over and over again:

Number, Carry
0b0011, 0b0
0b0110, 0b0
0b1100, 0b0
0b1000, 0b1
0b0001, 0b1
0b0011, 0b0

After 5 iterations we end up back where we started. Unimaginatively roll right works exactly the same, but shifts right.

Typically these operations are used to manipulate bits or perform simple multiplication/divisions.

Navigation
View posts[+24][+48][+96]