I am trying to add individual bits together for the s boxes in a DES cryptography program (if you don't know what that is, it doesn't matter.)
For example, I want to combine the first byte and the last byte, and the middle four bytes.
like:
take the byte 110011.
The first and last bytes combine to form 11.
the middle for bytes combine to form 1001.
This example is from <i> Applied Cryptography</i> by Bruce Schneier.
Right now I am turning my byte into a base 2 string, the string to a char array(this is probably kind of unnecessary), then concatenating the right chars back into two strings, and converting to bytes. This should work, but it's s really, really stupid idea.
Is there a better way to manipulate individual bits in a byte?
I am thinking of rewriting this in C++, but Java has better GUI APIs and I would need to learn how to make Java call a C++ class and retrieve the results.
Thanks.
Adding individual bits in a byte
Page 1 of 15 Replies - 589 Views - Last Post: 01 March 2012 - 08:49 PM
Replies To: Adding individual bits in a byte
#2
Re: Adding individual bits in a byte
Posted 01 March 2012 - 06:02 PM
Quote
For example, I want to combine the first byte and the last byte, and the middle four bytes.
You've got six bits here. Will it always be six?
Assuming yes, the following will do it for you. If using < Java 7, you'll need to take the numbers out of binary notation to get it to compile
int i = 0b110011;
int outerTwoBits = (i & 0b100000) > 0? 0b10 : 0;
outerTwoBits |= i & 1;
int middleBits = i >> 1 & 0b1111;
System.out.printf("outerTwoBits: %s\n", Integer.toBinaryString(outerTwoBits));
System.out.printf("middleBits: %s\n", Integer.toBinaryString(middleBits));
This post has been edited by g00se: 01 March 2012 - 06:32 PM
#4
Re: Adding individual bits in a byte
Posted 01 March 2012 - 08:01 PM
Thanks g00se, but I tried your example, but the compiler did not like the 0b- prefix. Do you have any other advice on other ways to initialize a binary integer?
I am compiling on a mac, so I don't know what exact version of the Java VM I have.
Your example looks like what I want, but I should brush up on my bitwise operators in Sheph's tutorial.
I think I will always have 6 bits in this stage of the DES algorithm.
I am compiling on a mac, so I don't know what exact version of the Java VM I have.
Your example looks like what I want, but I should brush up on my bitwise operators in Sheph's tutorial.
I think I will always have 6 bits in this stage of the DES algorithm.
#5
Re: Adding individual bits in a byte
Posted 01 March 2012 - 08:27 PM
0b prefix is a JRE 7.0 feature. If you have JDK 6.x just replace it by its hex equivalent and it will work
0b00000000 == 0x00;
0b00000001 == 0x01;
0b00000010 == 0x02;
0b00000011 == 0x03;
0b00000100 == 0x04;
0b00000101 == 0x05;
0b00000110 == 0x06;
0b00000111 == 0x07;
0b00001000 == 0x08;
0b00001001 == 0x09;
0b00001010 == 0x0A;
0b00001011 == 0x0B;
0b00001100 == 0x0C;
0b00001101 == 0x0D;
0b00001110 == 0x0E;
0b00001111 == 0x0F;
...
0b11111111 == 0xFF
0b00000000 == 0x00;
0b00000001 == 0x01;
0b00000010 == 0x02;
0b00000011 == 0x03;
0b00000100 == 0x04;
0b00000101 == 0x05;
0b00000110 == 0x06;
0b00000111 == 0x07;
0b00001000 == 0x08;
0b00001001 == 0x09;
0b00001010 == 0x0A;
0b00001011 == 0x0B;
0b00001100 == 0x0C;
0b00001101 == 0x0D;
0b00001110 == 0x0E;
0b00001111 == 0x0F;
...
0b11111111 == 0xFF
#6
Re: Adding individual bits in a byte
Posted 01 March 2012 - 08:49 PM
Or if you don't want to think about it, let the computer do the work
int[] bits = new int[8]; // 8 bits in a byte bits[0] = 1; // 0000 0001 for (int i = 1; i < 8; i++) // initialize all the bits bits[i] = bits[i - 1] * 2; /* * Now bit[0] represents a number with only the rightmost bit on, and * bit[1] is the second to rightmost bit. etc... To get any combination, * just | them together. */ int y = 51; // which is 0b110011 in JRE 7 int x = bits[0] | bits[1] | bits[4] | bits[5]; // x and y are the same, let's prove it System.out.println(x == y);
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|