Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:41:33+00:00 2026-05-30T08:41:33+00:00

Is it possible to implement a function that results in this mapping: { (0x01,

  • 0

Is it possible to implement a function that results in this mapping:

{
  (0x01, 0x01),
  (0x10, 0x10),
  (0x11, 0x00)
}

Using only bitwise operations?

Context

In the Flixel framework, there are a set of four constants,

FlxObject.LEFT:uint   = 0x0001;
FlxObject.RIGHT:uint  = 0x0010;
FlxObject.UP:uint     = 0x0100;
FlxObject.DOWN:uint   = 0x1000;

Obviously designed to be manipulated with bitwise operators. I was trying to write a function, using only bitwise operators, that would return the opposite direction of whatever was passed in (in terms of these FlxObject constants).

Some example mappings:

{
    (0x0110, 0x1001),
    (0x0100, 0x1000),
    (0x1010, 0x0101),
    (0x0001, 0x0010),
    (0x1100, 0x0000)
}

The problem is, my solution tends to break down when you pass it something like 0x0011, 0x1100, 0x1110 and similar, and requires a check against this case.
Testing code here (also at http://pastie.org/3420169):

#!/usr/bin/env python

from sys import stdout
from os import linesep

# Implementation without conditional
def horiz(dir):
    return(dir ^ 0x0011 ^ 0x1100) & 0x0011

def vert(dir):
    return (dir ^ 0x1100 ^ 0x0011) & 0x1100
    
def oppositeDirection(dir):
    return horiz(dir) | vert(dir)


# Implementation with conditional
def horizFix(dir):
    dir = horiz(dir)
    return dir if dir != 0x0011 else 0

def vertFix(dir):
    dir = vert(dir)
    return dir if dir != 0x1100 else 0

def oppositeDirectionFix(dir):
    return horizFix(dir) | vertFix(dir)

failcount = 0
testcount = 0

def test(dir, expect, func):
    global failcount, testcount
    testcount += 1
    result = func(dir)
    stdout.write('Testing: {0:04x} => {1:04x}'.format(dir, result))
    if result != expect:
        stdout.write('\t GOT {0:04x} expected {1:04x}'.format(result, expect))
        failcount += 1
    stdout.write(linesep)

test_cases =[0x0000, 0x0001, 0x0010, 0x0100, 0x1000, 0x0011, 0x0101, 0x1001, 0x0110, 0x1010, 0x1100, 0x0111, 0x1011, 0x1101, 0x1110, 0x1111] 


print 'Testing full oppositeDirection function----------------'
for case in test_cases:
    test(case, oppositeDirectionFix(case), oppositeDirection)

print '\nTesting horiz function---------------------------------'
for case in test_cases:
    test(case, horizFix(case), horiz)

print '\nTesting vert function----------------------------------'
for case in test_cases:
    test(case, vertFix(case), vert)


print '{0}Succeeded: {2}/{1}, Failed: {3}/{1}'.format(linesep, testcount, testcount - failcount, failcount)

If you run the test, you’ll see that in the cases like 0x0011 and 0x0000 horiz will return 0x0011, and for 0x1100 and 0x000 vert will return 0x1100. So close!

This is clearly an incredibly insignificant problem, and there will never be any situation in my game code where a direction value would be simultaneously left and right or up and down. But, I’m taking this as an opportunity to hone my bit twiddling skills. Is there some logic principle I’m missing here that will help me either solve it or realize it’s an unsolvable problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:41:34+00:00Added an answer on May 30, 2026 at 8:41 am

    No, you have to make some kind of test because your result depends on two adjacent bits.

    You could use XOR with 1111 and then test if the result contains 1100 or 0011.

    But since you only have 16 values to verify would it not be simpler to make a switch/select/match function for the 8 valid values?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQLServer 2008 I currently have a function that implements functionality as shown below
Possible Duplicate: The most efficient way to implement an integer based power function pow(int,
I don't believe this is possible, and searching on Google didn't yield any results,
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
Is it possible to implement autoboxing for your own classes? To illustrate my example,
Is it possible to implement Bluetooth devices to provide conference rather than one-to-one operation.
Is it possible to implement batching of multiple stored procedure calls (doing updates/deletes) in
Is it possible to implement McCarthy's amb -operator for non-deterministic choice in C#? Apparently
It's possible to implement INotifyCollectionChanged or other interface like IObservable to enable to bind
Is it possible to implement long press in JavaScript (or jQuery)? How? (source: androinica.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.