Digital and Interactive
Games 2015, Term 2, Session 2
Bitwise Operations
Queue
& - and
| - or
XOR – xor
! - not
P q AND OR XOR NOT
0 0 0 0 1
0 1 0 1 0
1 0 0 1 0
1 1 1 1 1
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S003_zpshaut6ljy.png
Ex
Bit 7 – 1 indicates the top of the door
Bit 3 – 0 indicates the bottom of the door
– left hinge
top/bottom, left/right hinge
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S004_zpsdv2cwffu.png
int topLeft = 0; 10001000
0000 0000 0000 0000
MSB LSB
topLeft = 128 + 8; // set bit 7 and 3 on.
// check if bit 7 is on
if (topLeft & 128 == 128)
Shift left 10001000
--------------
00010000
---
char ch;
for (int i = 0; i < 10; i++) {
ch = (char) ('x' + i);
System.out.print(ch);
ch = (char) ((int) ch OR 65503);
System.out.print(ch);
}
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
char aDoor;
// bit 7 - top part of the door
// - bottom part of the door
// bit 3 – 1 – left hinge
//
aDoor = 0;
aDoor = (char) ((int) aDoor | 128);
aDoor = (char) ((int) aDoor | 8);
Turn on - all to zero except the target bit, then OR it.
i.e. turn bit 3 on.
Var | 00001000
Turn off – all to one except target bit, then AND it.
i.e. turn bit 6 off
var & 10111111
Textbook Ch 5.
System.out.print(aDoor);
System.out.print(int) aDoor & (256 – 8);
System.out.print(int) aDoor;
// Turn it back on
aDoor = (char) ((int) aDoor | 8);
System.out.println();
// Shift 1 bit
aDoor = (char) ((int) aDoor << 1);
System.out.print((int) a Door);
System.out.print(ol);
aDoor = (char) ((int) aDoor >> 2);
System.out.print((int aDoor);
Queue
<a href="http://s483.photobucket.com/user/Brenorenz/media/W10S005_zpsyxnsfo3h.png.html" target="_blank"><img src="http://i483.photobucket.com/albums/rr193/Brenorenz/W10S005_zpsyxnsfo3h.png" border="0" alt=" photo W10S005_zpsyxnsfo3h.png"/></a>
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S005_zpsyxnsfo3h.png
remove + 20 is at position 0.
property: MAX_ITEMS
: item[] int
: begin int
: end int
: count int
Method : Add
Remove
get MaxItems : int
getCount
Search (int) : int
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S006_zpsda3c68xm.png
INVALID
– 99999
Que Sample
Queue
public class Queue {
private int MAX_ITEMS = 20;
private int count = 0;
private int begin = 0;
private int end = 0;
private int INVALID = 99999;
public void print() {
}
public boolean add(int value) {
boolean result = false;
if (count < MAX_ITEMS) {
item[end] = value;
++end;
result = true;
}
++ count;
return result;
}
public int getMaxItems() {
return MAX_ITEMS;
}
public int getCount() {
return count;
}
…
Test cases
Overall scenarios
– working
– expected errors
(1) Add an item to an empty queue
Values: 5, 10, 0, -20
Expected : B : MaxItem = 2 count = 0 peek = 99999
A : MaxItem = 2 count = 0 peek = 99999
print →
B : MaxItem = 20 count = 1 peek = 5
A : MaxItem = 20 count = 1 peek = 5
print
(2)
Add 20 items
Then add 1 item
B:
print
A: print
Stack
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S007_zpssjrbmotw.png
Queue.java (part of)
public class Queue {
private int MAX_ITEMS = 20;
private int count = 0;
private int item[] = new int{MAX_ITEMS];
private int begin = 0;
private int end = 0;
private int INVALID = 99999;
int index = begin;
for (int i=0; i< count; ++i) {
System.out.print[index] + “ ”);
++index;
if (index == this.MAX_ITEMS) {
index = 0;
}
}
}
…
#gamedesign #gameprogramming #java #minecraft #programming #tafe
Bitwise Operations
Queue
& - and
| - or
XOR – xor
! - not
P q AND OR XOR NOT
0 0 0 0 1
0 1 0 1 0
1 0 0 1 0
1 1 1 1 1
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S003_zpshaut6ljy.png
Ex
Bit 7 – 1 indicates the top of the door
Bit 3 – 0 indicates the bottom of the door
– left hinge
top/bottom, left/right hinge
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S004_zpsdv2cwffu.png
int topLeft = 0; 10001000
0000 0000 0000 0000
MSB LSB
topLeft = 128 + 8; // set bit 7 and 3 on.
// check if bit 7 is on
if (topLeft & 128 == 128)
Shift left 10001000
--------------
00010000
---
char ch;
for (int i = 0; i < 10; i++) {
ch = (char) ('x' + i);
System.out.print(ch);
ch = (char) ((int) ch OR 65503);
System.out.print(ch);
}
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
char aDoor;
// bit 7 - top part of the door
// - bottom part of the door
// bit 3 – 1 – left hinge
//
aDoor = 0;
aDoor = (char) ((int) aDoor | 128);
aDoor = (char) ((int) aDoor | 8);
Turn on - all to zero except the target bit, then OR it.
i.e. turn bit 3 on.
Var | 00001000
Turn off – all to one except target bit, then AND it.
i.e. turn bit 6 off
var & 10111111
Textbook Ch 5.
System.out.print(aDoor);
System.out.print(int) aDoor & (256 – 8);
System.out.print(int) aDoor;
// Turn it back on
aDoor = (char) ((int) aDoor | 8);
System.out.println();
// Shift 1 bit
aDoor = (char) ((int) aDoor << 1);
System.out.print((int) a Door);
System.out.print(ol);
aDoor = (char) ((int) aDoor >> 2);
System.out.print((int aDoor);
Queue
<a href="http://s483.photobucket.com/user/Brenorenz/media/W10S005_zpsyxnsfo3h.png.html" target="_blank"><img src="http://i483.photobucket.com/albums/rr193/Brenorenz/W10S005_zpsyxnsfo3h.png" border="0" alt=" photo W10S005_zpsyxnsfo3h.png"/></a>
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S005_zpsyxnsfo3h.png
remove + 20 is at position 0.
property: MAX_ITEMS
: item[] int
: begin int
: end int
: count int
Method : Add
Remove
get MaxItems : int
getCount
Search (int) : int
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S006_zpsda3c68xm.png
INVALID
– 99999
Que Sample
Queue
public class Queue {
private int MAX_ITEMS = 20;
private int count = 0;
private int begin = 0;
private int end = 0;
private int INVALID = 99999;
public void print() {
}
public boolean add(int value) {
boolean result = false;
if (count < MAX_ITEMS) {
item[end] = value;
++end;
result = true;
}
++ count;
return result;
}
public int getMaxItems() {
return MAX_ITEMS;
}
public int getCount() {
return count;
}
…
Test cases
Overall scenarios
– working
– expected errors
(1) Add an item to an empty queue
Values: 5, 10, 0, -20
Expected : B : MaxItem = 2 count = 0 peek = 99999
A : MaxItem = 2 count = 0 peek = 99999
print →
B : MaxItem = 20 count = 1 peek = 5
A : MaxItem = 20 count = 1 peek = 5
(2)
Add 20 items
Then add 1 item
B:
A: print
Stack
http://i483.photobucket.com/albums/rr193/Brenorenz/W10S007_zpssjrbmotw.png
Queue.java (part of)
public class Queue {
private int MAX_ITEMS = 20;
private int count = 0;
private int item[] = new int{MAX_ITEMS];
private int begin = 0;
private int end = 0;
private int INVALID = 99999;
int index = begin;
for (int i=0; i< count; ++i) {
System.out.print[index] + “ ”);
++index;
if (index == this.MAX_ITEMS) {
index = 0;
}
}
}
…
#gamedesign #gameprogramming #java #minecraft #programming #tafe
No comments:
Post a Comment