XOR in java is not providing binary values - java

This is my code . I want a binary value for c ,but my output is 294977 . How to xor this?
public class Dumm {
public static void main(String []args) {
int a = 01101010;
int b = 00001111;
int c = a ^ b;
System.out.print(c);
}
}

If you want to take a and b as binary value then start with "0b".
for print binary value use "Integer.toBinaryString()" method.
Try this:
public static void main(String [] args)
{
int a = 0b1101010;
int b = 0b0001111;
int c = a ^ b;
System.out.println(Integer.toBinaryString(c));
}

If you want to treat your numbers as binary, you have to start numbers with 0b.
You have prepended o(English Alphabet) instead of 0(Zero).
int a = 0b01101010;
int b = 0b00001111;
int c = a ^ b;
System.out.print(Integer.toBinaryString(c));

Related

How to implement 'BigInteger to Integer and vice-versa' in my code,unable to solve incompatable type error

I tried using type long in java it worked well. But, I want to handle values greater than long so I moved to BigInteger but ended up with an error with incompatible types(BigInteger and int).
Thanks in advance.
import java.io.*;
import java.util.*;
import java.math.BigInteger;
class Main
{
public static BigInteger aryaStarkToWin(int p, int r,BigInteger[][]req_Array) {
BigInteger a=new BigInteger("1000000007");
BigInteger b=new BigInteger("0");
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
BigInteger returnValue= req_Array[p][0];
returnValue=returnValue.mod(a);
return returnValue ;
}
public static BigInteger wins(int aryaCounter, int sansaCounter, BigInteger chances, final int p, final int r,BigInteger[][]req_Array) {
BigInteger a=new BigInteger("1000000007");
BigInteger b=new BigInteger("0");
BigInteger c=new BigInteger("1");
BigInteger d=new BigInteger("-1");
if(req_Array[aryaCounter][sansaCounter]!=d) {
return req_Array[aryaCounter][sansaCounter];
}
if (aryaCounter < sansaCounter * p) {
return b;
}
if (aryaCounter + sansaCounter == r) {
return c;
}
req_Array[aryaCounter][sansaCounter]=(wins(aryaCounter + 1, sansaCounter, chances, p, r,req_Array).mod(a).add(wins(aryaCounter, sansaCounter + 1, chances, p, r,req_Array).mod(a))).mod(a);
return req_Array[aryaCounter][sansaCounter];
}
public static void main(String[] args) {
int T_cases=0;
Scanner scany= new Scanner(System.in);
T_cases=scany.nextInt();
BigInteger d=new BigInteger("-1");
while(T_cases-->0) {
int no_of_rounds = scany.nextInt();
BigInteger[][] req_Array = new BigInteger[no_of_rounds+1][no_of_rounds+1];
int ptimes=scany.nextInt();
for(int i=0;i<=no_of_rounds;i++) {
for(int j=0;j<=no_of_rounds;j++) {
req_Array[i][j]=d;
}
}
System.out.println(aryaStarkToWin(ptimes,no_of_rounds,req_Array));
}
scany.close();
}
}
The input is
2
4 2
2 2
The output has to be
3
1
But, I'm getting error messages such as
Main.java:9: error: incompatible types: int cannot be converted to BigInteger
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
1 error
The Error message is clear. Third argument expect an instance of BigInteger while calling the method wins
You are calling the method as below:
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
^
And the method signature is:
public static BigInteger wins(int aryaCounter, int sansaCounter, BigInteger chances, final int p, final int r,BigInteger[][]req_Array) {
^^^^^^^^
The third argument you could pass in as below:
req_Array[p][0]=wins(p, 0, BigInteger.ZERO, p, r,req_Array) ;
or change the method signature as:
public static BigInteger wins(int aryaCounter, int sansaCounter, int chances, final int p, final int r,BigInteger[][]req_Array) {
Having said that, you aren't using chances in your method, so better to get rid of the same.

How to get the corresponding character from keysym code?

I am developing a Java API which is consumed by an angular app. There's a socket which sends all the key events from browser. How can I get the corresponding character of the keysym table?
Something like:
int a = 97;
int b = 98;
int c = 99
int enter = 65293;
String character = Keysym.get(a); //outputs a
Solution
You simply need to cast to char :
static char getCode(int key) {
return (char) key;
}
Use as
public static void main(String[] args) {
System.out.println(getCode(97)); // a
System.out.println(getCode(98)); // b
System.out.println(getCode(99)); // c
}

swapping of numbers using index in java is not working

package dspermutation;
import java.util.Scanner;
public class DSPermutation {
String s;
char[] c;
int n;
public static void main(String[] args) {
DSPermutation ds=new DSPermutation();
ds.input();
}
private void input() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
s=sc.next();
c=s.toCharArray();
n=c.length;
permutation(c,n-1,0);
}
private void permutation(char[] cc,int nn,int ii) {
if(ii==nn)
{
System.out.println(cc);
}
else
{
for(int j=ii;j<=nn;j++)
{
swap(cc[ii],cc[j]);
permutation(cc,nn,ii+1);
swap(cc[ii],cc[j]);
}
}
}
private void swap(char p, char c0) {
int x=s.indexOf(p);
int y=s.indexOf(c0);
/*1*/ char temp=c[x];
/*2*/c[x]=c[y];
/*3*/c[y]=temp;
/*c[x]=c0;
c[y]=p;*/
}
}
The above program is for printing all permutations of a given string.The result is coming true but in swap() method if i replace line 1,2,3(written in comment) by logic written in comment(after line 1,2,3) then answer comes wrong. Why could this be happening?
Your mistake is assuming c[x] == p and c[y] == c0. But the indexes x and y are derived from the immutable string s, which doesn't reflect the values in c in its shuffled state.
You are swapping values of character array using immutable string's position (i.e String always holds the same initial values). To make your commented code work you have to add this s = String.valueOf(c);at the end of swap function.
private void swap(char p, char c0) {
int x = s.indexOf(p);
int y = s.indexOf(c0);
// char temp = c[x];
// c[x] = c[y];
// c[y] = temp;
c[y] = p;
c[x] = c0;
s = String.valueOf(c);
}

A method which accepts two integer values as input parameters and returns the boolean

This is one of the practice questions of a test:
Write a method which accepts two integer values as input parameters and returns the boolean result true if the sum of the inputs is greater than or equal to 10 (and falseotherwise)
My answer is below but I don't think it looks correct. Can anyone give me a pointer?
public class Bruh{
public static void main (String [] arg){
int a;
int b;
boolean sum = true;
if ( a+b > 10)
System.out.println ("yo");
else{
sum = false;
}
}
}
You only wrote some code in the main method but you did not create one.
In order to do that you need to actually create a method in your Bruh class like:
public static boolean isSumGreaterThan9(int a, int b){
return (a + b) > 9;
}
Than call it from the main method:
public static void main (String [] arg){
int a = 4; // or whatever
int b = 7; // or whatever
System.out.println(isSumGreaterThan9(a, b));
}
You need to put your logic into a method and change your comparison to >= as per the requirement:
public static boolean isSumGreaterThanOrEqualToTen(int a, int b) {
return (a + b) >= 10;
}

Using local-variables in other methods?

I was trying to make a program that searches for a random number, but i had problems importing the "a" variable in the other method. I would be happy if i could get some explanation. I have already tried to make the variable static, but that doesn't work
import java.util.Random;
public class verschlüsselung {
private static void nummber(int a) {
Random r = new Random();
a = r.nextInt(999);
System.out.println(a);
}
private static void search(int b) {
b = 0;
if(b =! a) {
for(b = 1; b =! a ; b++) {
if(b == a) {
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
nummber(0);
search(0);
}
}
There is no such thing as using local variables in other methods.
You can return the variable from one method. Than call this method from other and get there the variable.
Declare the variable 'a' to be static and remove the parameter 'a' passed in the nummber()
function. This function does not need any input as it assigns the value of a random number to the global static variable 'a' which is accessed in the method search().
your declaration and method signature should read :
private static int a;
private static void nummber(){....}
May this help you:
private static int nummber( int a){
Random r = new Random();
a =r.nextInt(999);
System.out.println(a);
return a;
}
private static void search(int b, int a){
b = 0;
if(b =! a){
for(b =1; b =! a ; b++){
if(b == a){
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
int a = nummber(0);
search(0, a);
}

Categories

Resources