Initial Commit of my bit field demo
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
6
Java/Bit Fields/.classpath
Normal file
6
Java/Bit Fields/.classpath
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
17
Java/Bit Fields/.project
Normal file
17
Java/Bit Fields/.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Bit Fields</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
11
Java/Bit Fields/.settings/org.eclipse.jdt.core.prefs
Normal file
11
Java/Bit Fields/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.7
|
||||||
124
Java/Bit Fields/src/com/test/BitField.java
Normal file
124
Java/Bit Fields/src/com/test/BitField.java
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
/**
|
||||||
|
* BitFieldDemo.java
|
||||||
|
* @date Mar 16, 2012
|
||||||
|
* @author ricky barrette
|
||||||
|
* @author Twenty Codes, LLC
|
||||||
|
*
|
||||||
|
* Copyright 2012 Richard Barrette
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License
|
||||||
|
*/
|
||||||
|
package com.test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a simple bit field demo class
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class BitField {
|
||||||
|
|
||||||
|
private int mFlags;
|
||||||
|
|
||||||
|
public static final int FLAG_ONE = 1;
|
||||||
|
public static final int FLAG_TWO = 2;
|
||||||
|
public static final int FLAG_THREE = 4;
|
||||||
|
public static final int FLAG_FOUR = 8;
|
||||||
|
public static final int FLAG_FIVE = 16;
|
||||||
|
public static final int FLAG_SIX = 32;
|
||||||
|
public static final int FLAG_SEVEN = 64;
|
||||||
|
public static final int FLAG_EIGHT = 128;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BitFieldDemo
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public BitField(){
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BitFiledDemo
|
||||||
|
* @param flags
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public BitField(int flags) {
|
||||||
|
setFlags(flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the flags
|
||||||
|
*/
|
||||||
|
public int getFlags() {
|
||||||
|
return new Integer(mFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param flag being checked
|
||||||
|
* @return true if the flag has been set
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public boolean isFlagSet(int flag){
|
||||||
|
return (this.mFlags & flag) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses XOR logic to unset flags.
|
||||||
|
* Warning: this will set flags if they are NOT already set
|
||||||
|
* @param flags to be removed
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public void removeFlags(int flags){
|
||||||
|
if(flags < 0 || flags > 255)
|
||||||
|
throw new IllegalArgumentException("Invald flags");
|
||||||
|
this.mFlags = this.mFlags ^ flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses OR logic to set flags
|
||||||
|
* @param flags the flags to set
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public void setFlags(int flags) {
|
||||||
|
if(flags < 0 || flags > 255)
|
||||||
|
throw new IllegalArgumentException("Invald flags");
|
||||||
|
this.mFlags = this.mFlags | flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#toString()
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuffer s = new StringBuffer();
|
||||||
|
s.append("Flags = ");
|
||||||
|
s.append(this.mFlags);
|
||||||
|
s.append(", Bit 1 = ");
|
||||||
|
s.append(isFlagSet(FLAG_ONE));
|
||||||
|
s.append(", Bit 2 = ");
|
||||||
|
s.append(isFlagSet(FLAG_TWO));
|
||||||
|
s.append(", Bit 3 = ");
|
||||||
|
s.append(isFlagSet(FLAG_THREE));
|
||||||
|
s.append(", Bit 4 = ");
|
||||||
|
s.append(isFlagSet(FLAG_FOUR));
|
||||||
|
s.append(", Bit 5 = ");
|
||||||
|
s.append(isFlagSet(FLAG_FIVE));
|
||||||
|
s.append(", Bit 6 = ");
|
||||||
|
s.append(isFlagSet(FLAG_SIX));
|
||||||
|
s.append(", Bit 7 = ");
|
||||||
|
s.append(isFlagSet(FLAG_SEVEN));
|
||||||
|
s.append(", Bit 8 = ");
|
||||||
|
s.append(isFlagSet(FLAG_EIGHT));
|
||||||
|
return s.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
73
Java/Bit Fields/src/com/test/Main.java
Normal file
73
Java/Bit Fields/src/com/test/Main.java
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* Main.java
|
||||||
|
* @date Mar 16, 2012
|
||||||
|
* @author ricky barrette
|
||||||
|
* @author Twenty Codes, LLC
|
||||||
|
*
|
||||||
|
* Copyright 2012 Richard Barrette
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License
|
||||||
|
*/
|
||||||
|
package com.test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will be used to drive a class that uses bit fields
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Main instance
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public Main() {
|
||||||
|
/*
|
||||||
|
* Create a new bit field with the 8th bit set
|
||||||
|
*/
|
||||||
|
BitField b = new BitField(BitField.FLAG_EIGHT);
|
||||||
|
System.out.println(b.toString());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* set the 4th and 7th bits
|
||||||
|
*/
|
||||||
|
b.setFlags(BitField.FLAG_FOUR | BitField.FLAG_SEVEN);
|
||||||
|
System.out.println(b.toString());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* un-set the 8th bit
|
||||||
|
*/
|
||||||
|
b.removeFlags(BitField.FLAG_EIGHT);
|
||||||
|
System.out.println(b.toString());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* set the first abd second bits
|
||||||
|
*/
|
||||||
|
b.setFlags(BitField.FLAG_ONE | BitField.FLAG_TWO);
|
||||||
|
System.out.println(b.toString());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* unset the fourth and sencond bit
|
||||||
|
*/
|
||||||
|
b.removeFlags(BitField.FLAG_FOUR | BitField.FLAG_TWO);;
|
||||||
|
System.out.println(b.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the main method
|
||||||
|
* @param args
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Main();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user