Useful Bitwise tips

When it comes to maintaining state of an entity, bitwise operator plays an important role.Consider that you are developing a game and your character can have jump, walk, fire and run states. The characters can be in more than 2 states at any point of time.You can store this information easily using bitwise operators.

Define states of the player.

typedef enum {
eNone = 0, (0000 0000)
eRunning =1, (0000 0001)
eJumping =2, (0000 0010)
eWalking =4, (0000 0100)
eShooting =8 (0000 1000)
}EPlayerState;

Consider you want to set the player state to Running, you can do this by ORing.

0000 0000
0000 0001     (eRunning)
------------  (Bitwise OR)
0000 0001
------------

You can set another state like Shooting.

0000 0001     (Current state)
0000 1000     (eShooting)
------------  (Bitwise OR)
0000 1001
------------

We have captured both the states of the player.
To query the state ‘x’ of the player, you simply need to AND it.
To check if the player is Running or not,

0000 1001     (Current State)
0000 0001     (eRunning)
-----------   (Bitwise AND)
0000 0001
-----------

To reset any state, you need complement the state and then AND it.
To reset ‘Shooting’ state,

Complement the Shooting state.

0000 1000 -> 1111 0111 (complement)

Now AND it. This will reset only the ‘Shooting’ state and retain all the other states of the player.

0000 1001     (Current State)
1111 0111
-----------   (Bitwise AND)
0000 0001
-----------

These are the convenient methods.

-(void)setState:(EPlayerState)state
{
_playerSate = _playerSate|state;
}

-(void)resetState:(EPlayerState)state
{
_playerSate = _playerSate & (~state);
}

-(BOOL)isStateSet:(EPlayerState)state
{
BOOL isSet = _playerSate & state;
return isSet;
}

Connecting to Amazon EC2 server on Mac using Terminal app

Go to the folder where your .pem file is stored. You can retrieve the .pem file from your amazon account.

Launch the terminal app and run the  command

cd /folder/where/perm/file/stored/
ssh-add filename.pem

This should work fine most of the time and you should get a response similar to this.

Identity added:xxxxx

If the permission set on the .pem file aren’t correct then terminal will show this error

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0444 for 'filename.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
ec2-user@xx.xx.xx.xx:22: No such file or directory

To fix it, set the appropriate permissions.

chmod 400 filename.pem

Now, it’s time to connect to ec2 server.

ssh-add filename.pem
ssh ec2-user@xx.xx.xx.xx:22

If evrything works fine, then you should see this response on your terminal screen:

Last login: Tue Apr 23 03:34:27 2013 from xx.xx.xx.xx
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-ami/2012.09-release-notes/
There are 15 security update(s) out of 141 total update(s) available
Run "sudo yum update" to apply all updates.
Amazon Linux version 2013.03 is available.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)
[ec2-user@ip-xx-xx-xx-xx ~]$