12 Replies - 13879 Views - Last Post: 13 December 2009 - 04:46 AM Rate Topic: -----

#1 zerkz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 09

Read bit by bit in Java?

Posted 12 December 2009 - 02:05 PM

Hello. I have tried to google for this information, but people either instantly refer to the readByte() method in the various I/O packages or just say that it can't be done.

Anyways, I have ONE byte, in which each one of the 8 bits means a training for an employee in my data..

for instance...

The left most bit indicates orientation training
The 2nd bit from left indicates management training
The 3rd bit from left indicates technical training
The 4th bit from left indicates operations training
The 5th bit from left indicates administrative training
The 6th bit from left indicates quality control training
The 7th bit from left indicates sales training
The 8th bit from left indicates safety training

The problem is, I cannot figure out how to read in what trainings an employee has. If I had a readBit() method, I could easily use If Statements, etc. If the bit is switched on (1), then it means they have the training. This is the one stump I have left to overcome.

Thanks for your time.

Here is also a section of my code in which I would read the byte, and see what trainings the employees have, and set their various booleans to true (for each training, if they have it). This is also my "read" method.

public Employee readEmployee() throws FileNotFoundException,
			UnsupportedEncodingException, IOException {
		if (data.available() == 0) {
			return null;
		}
		String employeeId = "";// Starts Initialization of variables.
		char employeeIndicator;
		char departmentCode;
		String firstName = "";
		String lastName = "";
		float salary;
		int hireMonth;
		int hireDay;
		int hireYear;
				Byte trainingByte;
		int vacationDays; // End Initialization of variables.
		employeeId += (char) data.read(); // adds a character to the employeeId string from the file. 3 chars total.
		employeeId += (char) data.read();
		employeeId += (char) data.read();
		employeeIndicator = (char) data.read();
		departmentCode = (char) data.read();
		char temp = '0';

		for (int i = 0; i < 20; i++) { // reads the blank padded last name.
			temp = (char) data.read();
			if (temp != ' ') {
				lastName += temp;
			}
		}

		for (int i = 0; i < 15; i++) { // reads the blank padded first name.
			temp = (char) data.read();
			if (temp != ' ') {
				firstName += temp;
			}
		}
		salary = data.readFloat(); 
		hireMonth = data.read();
		hireDay = data.read();
		hireYear = data.readShort();
		Date hireDate = new Date(hireYear - 1900, hireMonth - 1, hireDay); // uses the variables above to create a date object.
		vacationDays = data.readShort();
				trainingByte = data.readByte();

		return new Employee(employeeId, employeeIndicator, departmentCode,
				lastName, firstName, salary, hireDate, vacationDays);
	}

This post has been edited by zerkz: 12 December 2009 - 02:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Read bit by bit in Java?

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 02:10 PM

They are right. Use the byte data type as well.
FileInputStream fis = new FileInputStream("filename");
DataInputStream dis = new DataInputStream(fis);
byte info = dis.getByte();


Now, you can use the if statements as you suggested to see if any of them are on.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 02:17 PM

Use my Eratostene sieve BitSetFile in the Code Snippet
It was designed to reads/write/rewrite a file bit per bit as a huge array of boolean
Was This Post Helpful? 0
  • +
  • -

#4 zerkz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 09

Re: Read bit by bit in Java?

Posted 12 December 2009 - 02:26 PM

View PostDogstopper, on 12 Dec, 2009 - 01:10 PM, said:

They are right. Use the byte data type as well.
FileInputStream fis = new FileInputStream("filename");
DataInputStream dis = new DataInputStream(fis);
byte info = dis.getByte();


Now, you can use the if statements as you suggested to see if any of them are on.


I am unsure of how this is to help me. I still need to be able to tell what each bit is.. It seems when I have it stored into a byte and have it print itself or look at the "variable" section on my IDE, that the byte is shown as a INT (example, the first byte is shown as 40, so I'm going to guess its 00101000. Since a employee can have multiple training though... I need to be able to seperate EACH bit and test the condition.
Was This Post Helpful? 0
  • +
  • -

#5 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 03:44 PM

You can do this using modulus and the bit shift operator.

boolean[] bits = boolean[8];

for(int i = 0; i < bits.length && someByte != 0; i++)
{
    if(someByte % 2 == 1)
    {
        boolean[i] = true;
    }

    someByte >>= 1;
}


That will create a boolean array of true's and false's, or more basically 1's and 0's.

EDIT: Sorry pbl, my bad. I always though it worked without that somehow, would explain why I've had troubles with bit shifting before though...

This post has been edited by TriggaMike: 12 December 2009 - 07:06 PM

Was This Post Helpful? 1

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 03:52 PM

View PostTriggaMike, on 12 Dec, 2009 - 02:44 PM, said:

You can do this using modulus and the bit shift operator.

boolean[] bits = boolean[8];

for(int i = 0; i < bits.length && someByte != 0; i++)
{
    if(someByte % 2 == 1)
    {
        boolean[i] = true;
    }

    someByte >> 1;
}


That will create a boolean array of true's and false's, or more basically 1's and 0's.

someByte >>= 1; :)
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 04:24 PM

To get your training type from the bitset you can do something like the following. It would be probably better still to use an enum:

public class Work {
	final static String[] TRAINING = {
	"ORIENTATION_TRAINING",
	"MANAGEMENT_TRAINING",
	"TECHNICAL_TRAINING",
	"OPERATIONS_TRAINING",
	"ADMINISTRATIVE_TRAINING",
	"CONTROL_TRAINING",
	"SALES_TRAINING",
	"SAFETY_TRAINING"

	};

	public static void main(String[] args) {
	System.out.println(Work.getTrainingType(Integer.valueOf(args[0])));
	}

	public static String getTrainingType(int bitset) {

	for(int i = 0;i < 32;i++) {	
		if((bitset & (1 << i)) != 0) {
		return TRAINING[i];
		}
	}
	return null;
	}
}


Was This Post Helpful? 1

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 05:20 PM

Of course, if more than one type of training is contained in the bitset, you should append to Set<String> or use EnumSet
Was This Post Helpful? 0
  • +
  • -

#9 zerkz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 09

Re: Read bit by bit in Java?

Posted 12 December 2009 - 05:27 PM

View PostTriggaMike, on 12 Dec, 2009 - 02:44 PM, said:

You can do this using modulus and the bit shift operator.

boolean[] bits = boolean[8];

for(int i = 0; i < bits.length && someByte != 0; i++)
{
    if(someByte % 2 == 1)
    {
        boolean[i] = true;
    }

    someByte >> 1;
}


That will create a boolean array of true's and false's, or more basically 1's and 0's.


Ok, this seems like the best solution for me. However, could you explain to me the coding of this? I have one byte, which I see you put in the form of some byte.. Modulus gives the "remainder" of division.. so, we divide that... but your if condition doesn't seem to be changing each loop... so wouldn't it be the same boolean for all bits? I am confused upon that part.


PS, No offense to the other, but i do not have a good knowledge of Enum or Set<String>.

This post has been edited by zerkz: 12 December 2009 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 05:38 PM

But why would you do that? All you'd be doing is transforming something that's already a bitset into another kind of bitset..?

	public static Set<String> getTrainingType(int bitset) {
	Set<String> result = new HashSet<String>;
	for(int i = 0;i < 32;i++) {	
		if((bitset & (1 << i)) != 0) {
		result.add(TRAINING[i]);
		}
	}
	return result;
	}


This post has been edited by g00se: 12 December 2009 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

#11 zerkz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 09

Re: Read bit by bit in Java?

Posted 12 December 2009 - 05:41 PM

View Postg00se, on 12 Dec, 2009 - 04:38 PM, said:

But why would you do that? All you'd be doing is transforming something that's already a bitset into another kind of bitset..?


I'm not sure how Enum/Set<string> or your solution works. I could understand what the other posters code was trying to do, but as I asked in my other post, it doesn't seem to test the condition for each bit (there doesn't seem to be any change in the if statement's conditions for each loopthrough).
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Read bit by bit in Java?

Posted 12 December 2009 - 06:48 PM

the >> and << operators shift the bits right or left by the amount specified on the right side of them
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: Read bit by bit in Java?

Posted 13 December 2009 - 04:46 AM

Quote

I'm not sure how Enum/Set<string> or your solution works.


The following might make it easier to see what happens:

	public static Set<String> getTrainingType(int bitset) {
	Set<String> result = new HashSet<String>;
	for(int i = 0;i < 32;i++) {
		int testbit = 1 << i;  

		System.out.printf("Now testing bit %d (value %d)\n", i, testbit);
		if((bitset & testbit0) != 0) {
		result.add(TRAINING[i]);
		}
	}
	return result;
	}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1