Cutstuff Forum

Mega Man 8-bit Deathmatch => Tutorial Collection => Topic started by: OGsus on December 03, 2013, 09:11:15 AM

Title: ACS, Signed Integers, and Bit Shifts
Post by: OGsus on December 03, 2013, 09:11:15 AM
Quote from: "*Alice"
Multiplication by 2^n is the same as bit shifting to the left (<<) by n.

Division by 2^n is the same as bit shifting to the right (>>) by n.

The ACS right-shift is what is called an "arithmetic shift", i.e. it preserves the sign.

Code: [Select]
ACS, Signed Integers, and BitShifts:
Test 0: 32
Int Value: 32
Binary Value:
 3         2         1         0
10987654321098765432109876543210
00000000000000000000000000100000

Test 1: 32 >> 2
Int Value: 8
Binary Value:
 3         2         1         0
10987654321098765432109876543210
00000000000000000000000000001000

Test 2: 32 << 2
Int Value: 128
Binary Value:
 3         2         1         0
10987654321098765432109876543210
00000000000000000000000010000000

Test 3: -32
Int Value: -32
Binary Value:
 3         2         1         0
10987654321098765432109876543210
11111111111111111111111111100000

Test 4: -32 >> 2
Int Value: -8
Binary Value:
 3         2         1         0
10987654321098765432109876543210
11111111111111111111111111111000

Test 5: -32 << 2
Int Value: -128
Binary Value:
 3         2         1         0
10987654321098765432109876543210
11111111111111111111111110000000
Title: Re: ACS, Signed Integers, and Bit Shifts
Post by: *Alice on December 03, 2013, 11:44:45 AM
Multiplication by 2^n is the same as bit shifting to the left (<<) by n.

Division by 2^n is the same as bit shifting to the right (>>) by n.

The ACS right-shift is what is called an "arithmetic shift", i.e. it preserves the sign.

@OGsus:
What is the rounding behaviour in ACS when using / on negative numbers? Towards 0 or negative infinity? If ACS rounds towards 0 that could lead to confusion, because >> always rounds towards negative infinity by definition.
Title: Re: ACS, Signed Integers, and Bit Shifts
Post by: OGsus on December 03, 2013, 09:07:37 PM
Division probably rounds towards zero if it doesn't actually round properly.  I'm too tired to test right now.  I'll post results later.