Basics – Binary Logic Cody Solutions

Basics - Binary Logic Cody Solutions

Learn Matlab and earn a badge and skill. This section contains the Cody solution of Basics – Binary Logic Cody Solution. Total 10 solved problems.

Visit the Matlab official Problems page of Basics – Binary Logic.

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

JOIN US ON TELEGRAM
Get Notified Faster

Problem 15. Find the longest sequence of 1's in a binary sequence.

Given a string such as
s = ‘011110010000000100010111’
find the length of the longest string of consecutive 1’s. In this example, the answer would be 4.
Example:
Input x = ‘110100111’
Output y is 3
function y = lengthOnes(x)
n=length(x);
p=1;
for p=1:n
    c(p)=0;
end
p=1;
for i=1:n
    if str2num(x(i))==1
        c(p)=c(p)+1;
    else
        p=p+1;
    end
end
y=max(c);
end

Problem 2522. Convert given decimal number to binary number.

Convert given decimal number to binary number.
Example x=10, then answer must be 1010.
function y = dec_bin(x)
  y = str2num(dec2bin(x));
end

Problem 2678. Find out sum and carry of Binary adder

Find out sum and carry of a binary adder if previous carry is given with two bits (x and y) for addition.
Examples
Previous carry is 1 and x=y=1. Addition is 1 and carry is also 1.
Previous carry is 0 and x=y=1. Addition is 0 and carry is also 1.
Previous carry is 0 and x=1, y=0. Addition is 1 and carry is also 0.
function [sum, carry] = bin_sum_carry(pc,x,y)
  sum = mod(x+y+pc,2);
  carry = mod(x*y+y*pc+pc*x,2);
end

Problem 34. Binary numbers

Given a positive, scalar integer n, create a (2^n)-by-n double-precision matrix containing the binary numbers from 0 through 2^n-1. Each row of the matrix represents one binary number. For example, if n = 3, then your code could return
>> binary_numbers(3)
ans =
1 1 1
0 0 0
0 1 1
0 1 0
0 0 1
1 0 0
1 1 0
1 0 1
The order of the rows does not matter.
function A = binary_numbers(n)
for i = 0:2^n-1
    A(i+1,:) = dec2bin(i,n)-'0';
end

Problem 108. Given an unsigned integer x, find the largest y by rearranging the bits in x

Given an unsigned integer x, find the largest y by rearranging the bits in x.
Example:
Input x = 10
Output y is 12
since 10 in binary is 1010 and 12 in binary is 1100.
function y=maxit(x)
x2=dec2bin(x);
x3=num2str(x2);
x4=sort(x3,'descend');
y=bin2dec(x4);
end

Problem 1295. Bit Reversal

Given an unsigned integer x, convert it to binary with n bits, reverse the order of the bits, and convert it back to an integer.
function y = bit_reverse(x,n)
  y = bin2dec(reverse(dec2bin(x,n)));
end

Problem 1547. Relative ratio of "1" in binary number

Input(n) is positive integer number
Output(r) is (number of “1” in binary input) / (number of bits).
Example:
  • n=0; r=0
  • n=1; r=1
  • n=2; r=0.5
function r = ones_ratio(n)
  b = dec2bin(n)-'0';
  r = sum(b)/length(b);
end

Problem 2891. Binary code (array)

Write a function which calculates the binary code of a number ‘n’ and gives the result as an array(vector).
Example:
Input n = 123
Output y =[1,1,1,1,0,1,1]
function y = dectobin(x)
  y = dec2bin(x)-'0';
end

Problem 43977. Converting binary to decimals

Convert binary to decimals.
Example:
010111 = 23.
110000 = 48.
function y = bin_to_dec(d)
  y = bin2dec(num2str(d));
end

Problem 2869. There are 10 types of people in the world

Those who know binary, and those who don’t.
The number 2015 is a palindrome in binary (11111011111 to be exact) Given a year (in base 10 notation) calculate how many more years it will be until the next year that is a binary palindrome. For example, if you are given the year 1881 (palindrome in base 10! :-), the function should output 30, as the next year that is a binary palindrome is 1911. You can assume all years are positive integers.
Good luck!!kcul dooG
function y = yearraey(x)
  c=0;
  while string(dec2bin(x)) ~= string(fliplr(dec2bin(x)))
      x=x+1;
      c=c+1;
  end
  y=c;
end

Basics on Cell Arrays 10 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

Leave a Comment

Your email address will not be published. Required fields are marked *

0

Tech Info