Java - Int,Short,Char binary operations - java

As im currently aware the following is correct:
char 8 bit value e.g 0 0 0 0 0 0 0 0
short 16 bit value e.g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
int 32 bit value e.g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I know the above sounds idiot proof but i want to desribe every step.
So i have the values 1 and 29 which are both 8 bits if im correct.
1: 0 0 0 0 0 0 0 1
29: 0 0 0 1 1 0 0 1
Now as these are 8 bits i can do the following
char ff = (char) 1;
char off = (char) 29;
So thats me storying my two values.
I now want to concation these values so it looks like
1 2 9 in binary that would be: 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1
Im currently doing:
short concat = (short) (ff | off)
But get the result 29 when it should be 285 as the binary would be
32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1
Where the hell im going wrong :(?
-- UPDATE CODE SOLUTION --
byte of= (byte) 29;
byte fm1 = (byte) 1;
char ph1 = (char) (fm1<<8 | of);
or is
short ph2 = (short) (fm1 <<8 | of);
Whats better as there both 16 bit?
System.out.println((int)ph1);

You need to shift the bits left, by 8.
short concat = (short) (ff <<8 | off)
The pipe is the bitwise or, so you just end up putting the same bits on the same place, putting 1s on places were either the first, or the second char has a 1.

char is two bytes
byte is one byte
byte ff = (byte) 1;
byte off = (byte) 29;
short concat = (ff << 8)|off;

Related

how to restricts the number of character and lines reads from a file before insertinting into 2dimentional array in java

i have go through the previous answers but nothing like what i am looking for, specifically in java. here is my code, the block of my code can read a singe character integer only which is not exactly what i intend to do, i intends to reads more than one char integer, it doesn't work. and i want reads only 16 lines and 16 integers in a line from the file even if the file contains more than 16 lines and more than 16 integers per line. can some one share an idea with me please?
Here is sample input data:
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4 0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4
i just want insert this into the 2dimentional array as you can see in my code, but my array is 16X16 but the file may be more than 16x16 in size, but i just want reads just 16x16 even if the file contains more than that, and ignore the empty line even if it exist.
BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
String line = null;
int[][] board = new int[16][16];
int k = 0;
while((line = bufferedReader.readLine())!=null) {
String[] newmatrix = line.split(" ");
for(int i=0; i<9; i++) {
board[k][i] = Integer.parseInt(newmatrix[i]);
}
k++;
}
This appears to be "a learning exercise". So, hints / advice only1:
To stop reading after 16 lines, use a counter.
Skip an empty line by testing for an empty line.
Use a Scanner (hasNextInt() and nextInt() to process each line.
It is a good idea to avoid hard-wiring literal constants into your code ... like 9 which you seem to have pulled out of the air.
Use board.length - 1 or board[i].length - 1 for your array bounds when "looping". (See previous)
Also ... your input file seems to only have 14 integers per line, not 16 as the question states.
1 ... because you will learn more by coding this yourself.
The code below should work.
BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
String line = null;
int[][] board = new int[16][16];
int k = 0;
while((line = bufferedReader.readLine())!=null) {
String[] newmatrix = line.split(" ");
for(int i = 0; i < 16; i++) {
board[k][i] = Integer.parseInt(newmatrix[i]);
}
k++;
if (k == 16)
break;
}
bufferedReader.close();

How to make InputStream skip over a line if it starts with '#'?

I am writing a program that has to decode QR Codes. The codes are internally represented as a 2D Boolean List. The codes will usually be read as text files containing 1s and 0s, where 1 means a dark module (true) in the matrix, and 0 a light module (false).
I have to implement a method, which will take an InputStream and then has to return a QR Code back. I'll have to read .txt which contains 1s, 0s and comment lines beginning with '#'. I need to make the method ignore those comment lines, but I am not sure how to do it.
Here are some relevant parts of the code:
First the QR Code constructor (All of the methods used in the constructor work and do their job):
public class QRCode {
private List<List<Boolean>> data; //A 2D Boolean matrix, true means black, false means white.
public QRCode(List<List<Boolean>> data)
{
if (data == null)
throw new NullPointerException();
if (QRCode.isQuadratic(data) == false)
throw new InvalidQRCodeException("Matrix must be quadratic!");
if (QRCode.versionCheck(data) < 1 || QRCode.versionCheck(data) > 40)
throw new InvalidQRCodeException("Invalid Dimensions (Version).");
if (QRCode.correctlyAlligned(data) != true)
throw new InvalidQRCodeException("Improper Allignment!");
if (QRCode.correctTimers(data) != true)
throw new InvalidQRCodeException("Incorrect Timing Pattern!");
if (QRCode.correctFormatting(data) != true)
throw new InvalidQRCodeException("Incorrect Formatting!");
this.data = data;
}
}
This is the method I'm referring to. What I wrote sofar at least. Also, if the .txt file contains anything other than 1s, 0s and comment, it should throw an exception.
PS: I've never used InputStreams before, I tried googling this but all answers I found were for specific types of Streams, and they use a .readLine() method, which my IS here is not letting me use.
public static QRCode fromFile(InputStream is) throws IOException
{
int i;
List<List<Boolean>> data = new ArrayList<>(); //a 2D Boolean Matrix
int y = -1, x = -1;
if (is == null)
throw new NullPointerException();
while((i = is.read())!=-1) //Reading begin
{
if (i == (byte) '\n') //If a line in .txt file ends.
{
y++;
data.add(new ArrayList<Boolean>());
x = 0;
}
if ((char) i == '1') //|| (char) i == '0')
{
data.get(y).add(true);
x++;
}
if ((char) i == '0') //||
{
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 0 0 0 0 0 1 0 0 0 1 1 0 1 1 0 1 0 0 0 1 1 0 0 0
1 0 1 1 1 0 1 0 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1
1 0 1 1 1 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 0 0 1 1 1
1 0 1 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 0 1 1
1 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1
1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 1
First, I suggest you use a Reader to read text. Then you can use a BufferedReader with its readLine() method to work with lines of text, rather than a stream of bytes.
Regardless, given your current code: once your read a \n, set a boolean flag to indicate that you've just saw a new line character. Then, as you begin reading the next line, switch on that flag such that if it's true, and you see a # as the next character, you should read until you see another \n. If it the flag is not true, then read the rest of the line as you are doing.
You may want to consider whitespace when finding the #, depending how lax you want this to be.
Why not use Files.lines instead of InputStream?
Files.lines(Paths.get("path_to_File.txt")).filter(s -> !s.contains("#"));// let's
//read all lines form the file and skip the lines with `#` symbol
Than we can convert each line to a list of Boolean:
s -> Stream.of(s.split(" ")).map("1"::equals).collect(Collectors.toList())// split
string by ` ` (space) symbol and convert to Boolean "1" - true, "0" - false
Now let's put everything together:
List<List<Boolean>> qr = Files.lines(Paths.get("data/fromFile.txt"))
.filter(s -> !s.contains("#")).map(
s -> Stream.of(s.split(" ")).map("1"::equals)
.collect(Collectors.toList())
).collect(Collectors.toList());
For the file:
# name: small
# type: bool matrix
# rows: 1
# columns: 3
1 1 1
1 0 1
Output will be
[true, true, true]
[true, false, true]
If you decide to use Reader then you may use regex to eleminate the lines which includes any character except '0' and '1'. You can find detail about regex usage below link.
How to check if a string contains only digits in Java
You can modify regex expression for only 1 and 0 like String regex = "[0-1]+";
After you can use below sample code to get each character.
String s1="hello";
char[] ch=s1.toCharArray();

How do I calculate negation(~) of -5

public class UnaryOperator {
public static void main(String[] args) {
byte a= -5;
System.out.println(~a); // prints 4
}
}
When I do it manually, I get the answer as 6.
Here is how I did it:
128 64 32 16 8 4 2 1
0 0 0 0 0 1 0 1
As it is a negation I inverted it to the following:
128 64 32 16 8 4 2 1
0 0 0 0 0 1 0 1
sign -1 1 1 1 1 0 1 0
-----------------------------
0 0 0 0 1 0 1
add one--> 0 0 0 0 0 1 1
------------------------------
0 0 0 0 1 1 0 = 6
------------------------------
I know there's something wrong with what I am doing but I am not able to figure it out.
5 is 00000101
-5 is 11111010+00000001 = 11111011
~(-5) is 00000100
so you get 4.
You're starting out with -5, which is in two's complement. Thus:
-128 64 32 16 8 4 2 1
1 1 1 1 1 0 1 1 (= -5)
flip: 0 0 0 0 0 1 0 0 (= +4)
I haven't done much bitwise stuff, but after reading wikipedia for a few seconds it seems like NOT -5 = 4, on wikipedia they used NOT x = -x - 1. So the program is correct.
Edit: For unsigned integers, you use NOT x = y - x were y is the maximum number that integer can hold.

Java JPG DCT coefficients alter by themselfs after I modify them

I have a program that changes the DCT coefficients of a JPG image.
This is the code that gives me the DCT coefficients
public int[] quantizeBlock(double inputData[][], int code) {
int outputData[] = new int[blockSize * blockSize];
int i, j;
int index;
index = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
// The second line results in significantly better compression.
outputData[index] = (int) (Math.round(inputData[i][j]
* (((double[]) (Divisors[code]))[index])));
// outputData[index] = (int)(((inputData[i][j] * (((double[])
// (Divisors[code]))[index])) + 16384.5) -16384);
index++;
}
}
return outputData;
}
This is a DCT matrix before modifications
-43 7 0 0 0 0 0 0
-8 1 2 -1 0 0 0 0
-1 -1 -1 1 0 0 0 0
-2 1 0 -1 0 0 0 0
6 0 0 0 0 0 0 0
-2 0 1 0 0 0 0 0
-1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
This is after the modifications
-42 8 0 0 0 0 0 0
-7 1 3 0 0 0 0 0
0 0 0 1 0 0 0 0
-1 1 0 0 0 0 0 0
7 0 0 0 0 0 0 0
-1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
After I save the image using image Buffer,I use the created image to get back the modified DCT from it but all I get is:
-41 9 0 0 0 0 0 0
-6 1 4 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0
8 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
I've seen a question where the user using a library in IOS did the same thing and had the same problem.Apparently the library recopressed the image and the hidden message was destroyed.
I don't know if this is the case for me.I use Image Buffer to create the image.
A couple things off the top that could be happening. The first is rounding errors. The JPEG process introduces small errors. All your values are one off. This could come from rounding.
The second is quantization. Your values may be quantized (divided). Your example does not indicate the compression stages that may be taking place in between your examples.

Arithmetic operation so that 0, 1, & 2 return 0 | 3, 4, & 5 return 1, etc

I'm trying to take 9x9, 12x12, 15x15, etc. arrays and have the program interpret them as multiple 3x3 squares.
For example:
0 0 1 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 3 0
0 0 0 0 0 0 6 0 0
0 0 4 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0
0 0 0 0 8 0 0 0 9
Will be understood as:
0 0 1 | 0 0 0 | 0 0 0
0 0 0 | 0 0 2 | 0 0 0
0 0 0 | 0 0 0 | 0 3 0
------+-------+------
0 0 0 | 0 0 0 | 6 0 0
0 0 4 | 0 0 0 | 0 0 0
0 0 0 | 0 0 5 | 0 0 0
------+-------+------
0 0 0 | 0 0 0 | 0 0 0
0 7 0 | 0 0 0 | 0 0 0
0 0 0 | 0 8 0 | 0 0 9
Where:
"1" # [0][2] is in box "[0][0]"
"2" # [1][5] is in box "[0][1]"
...
"6" # [3][6] is in box "[1][2]"
...
"9" # [8][8] is in box "[2][2]"
.
I can use row % 3 and column % 3 to determine the row and column values within the box, but how can I determine which box a given value in the array is stored in?
This formula could be used in a method such as the one below.
public int[] determineCoordinatesOfBox(int rowInArray, int columnColumnInArray) {
// determine row value
// determine column value
// return new int[2] with coordinates
}
It seems possible and I've been beating my head over this. Perhaps I'm making a simple problem too difficult?
Many thanks for the help!
Justian
You're looking for the / operator:
box[0] = rowInArray / 3;
box[1] = columnInArray / 3;
If I understand correctly, it's just simple integer division.
Since you're coding Java (it would be the same in at least C, C++ and C#), it's simply / operator:
int rowInArray = 3;
int columnInArray = 7;
int boxY = rowInArray / 3; // will evaluate to 1
int boxX = columnInArray / 3; // will evaluate to 2
int rowInBox = rowInArray % 3; // will evaluate to 0
int columnInBox = columnInArray % 3; // will evaluate to 1
Just keep both the arguments of division integer - 7 / 3 is 2, but 7 / 3.0 or 7.0 / 3 will be 2.5.

Categories

Resources