diff --git a/Java/Bit Fields/.classpath b/Java/Bit Fields/.classpath new file mode 100644 index 0000000..fb565a5 --- /dev/null +++ b/Java/Bit Fields/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Java/Bit Fields/.project b/Java/Bit Fields/.project new file mode 100644 index 0000000..fdd7f89 --- /dev/null +++ b/Java/Bit Fields/.project @@ -0,0 +1,17 @@ + + + Bit Fields + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Java/Bit Fields/.settings/org.eclipse.jdt.core.prefs b/Java/Bit Fields/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/Java/Bit Fields/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Java/Bit Fields/src/com/test/BitField.java b/Java/Bit Fields/src/com/test/BitField.java new file mode 100644 index 0000000..0a492c2 --- /dev/null +++ b/Java/Bit Fields/src/com/test/BitField.java @@ -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(); + } +} \ No newline at end of file diff --git a/Java/Bit Fields/src/com/test/Main.java b/Java/Bit Fields/src/com/test/Main.java new file mode 100644 index 0000000..1f16c8d --- /dev/null +++ b/Java/Bit Fields/src/com/test/Main.java @@ -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(); + } +} \ No newline at end of file