Advanced Search

Author Topic: ACS, Signed Integers, and Bit Shifts  (Read 5090 times)

0 Members and 1 Guest are viewing this topic.

December 03, 2013, 09:11:15 AM
Read 5090 times

Offline OGsus

  • Standard Member
  • Date Registered: November 25, 2013, 05:44:32 PM

    • View Profile
ACS, Signed Integers, and Bit Shifts
« 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

December 03, 2013, 11:44:45 AM
Reply #1

Offline *Alice

  • Standard Member

  • summertime everywhere summertime in your hair
  • Date Registered: January 07, 2013, 11:56:48 PM

    • View Profile
    • aliceif#4000
Re: ACS, Signed Integers, and Bit Shifts
« Reply #1 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.

December 03, 2013, 09:07:37 PM
Reply #2

Offline OGsus

  • Standard Member
  • Date Registered: November 25, 2013, 05:44:32 PM

    • View Profile
Re: ACS, Signed Integers, and Bit Shifts
« Reply #2 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.