Thursday, 29 January 2015

Find the Binary Or in Strings using Java

public class BinaryOr { public static void main(String[] args) { String str1="0101"; String str2="1011"; int c=0; StringBuilder sb=new StringBuilder(); for (int i=0;i<str1.length();i++) { int fchar=str1.charAt(str1.length()-1-i)-'0'; int schar=str2.charAt(str2.length()-1-i)-'0'; int sum=fchar+schar+c; int rbit=sum%2; c=sum>>1; sb.insert(0,(char)(rbit+'0')); } if(c==1) sb.insert(0,(char)'1'); System.out.println(sb.toString()); } }

No comments:

Post a Comment