How to add a remainder and an integer? - mod

In my code, there is a variable that gets added 29 to its original number. If that number is bigger than 31 then the variable is supposed to be reset to 0 and the remainder should be added to that. For example, we start at 4 and we are adding 29 then it's 33. And that is bigger than 31 so it should be set to 0 + 2. How can I do this in java? I have 0 clues about what to do.

I am not sure if you meant to do this recursively until reach a certain condition. But I think this can give you an idea
public static int solution(int number){
int add29 = (number+29);
if(add29 > 31){
return add29%31;
}else{
return add29;
}
}

Related

Assigning a value to the pointer to an object

class Distance
{
public:
int a;
};
int main()
{
Distance d1; //declaring object
char *p=(char *)&d1;
*p=1;
printf("\n %d ",d1.a);
return 0;
}
This is my code.
When I am passing the value of 'a' to be like 256,512 , I am getting 257,513 respectively but for values like 1000 i get 769 and for values like 16,128,100 I am getting 1.
First I thought it might be related to powers of 2 being incremented by 1 due to changes in their binary representation. But adding 1 to binary representation of 1000 won't give me 769.
Please help me to understand this code.
*p = 1 sets the last byte(char) to 000000001
As you're type casting int to char,
binary for (int)1000 is (binary)0000001111101000
you're assigning (int)1 for last 8 bits i,e (binary)0000001100000001 which is 769.
Using 256512 worked because last 8 bit that you change are all zeros i.e (int)256512 is (binary)111110101000000000 so making last bit as 1 gives you (binary)111110101000000001 which is (int)256513
And I think(not sure) you get 1 for 16,128,100 because this integer is well out of int range and thus not assigned and a is set to 0 as class object is created. and thus setting last bit to 1 makes a = 1

Why code displays error?

Can you help me and explain why this code gives a error? I would like to use XOR, but I can not. I'm trying to do this using the following formula:"A XOR B= (A AND ~B)OR(~A AND B). Can you hint what did I do wrong?
public = 'public';
password = 'passwd';
if length(public)== length(password)
public = uint8(public);
password = uint8(password);
negpublic = ~(dec2bin(public));
negpassword = ~(dec2bin(password));
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
public = dec2bin(public);
password = char(password)
else
fprintf('length not ok!\n' );
end
Normally I do not provide answers for homework questions, but it seems as you are almost there. The logic is done and that was the important part I guess.
Regarding the code, there is a few of bugs here. The function dec2bin will deceit you. As far as I know, matlab does not support binary format. The dec2bin actually convert the number to an array of char :(. However, having the text in binary format is not a requirement for doing bitwise operations. I cannot really see the use for a binary format in matlab since the smallest data unit for most computer achitectures normally is one byte.
You can use the function bitcmp (bitwise complement, which is another word for bitwise NOT) to do the negation. Secondly, bitwise operations can also work on vectors. Third, it is possible to define the negation as a variable, but bit operations are among the cheapest for most processors and operating systems so this is frankly not necessary for only two uses. So the content of everything is that you can simplify things a lot.
ab = 'ab'; bb = 'bb';
ab=uint8(ab); bb=uint8(bb);
bitor(bitand(ab,bitcmp(bb)), bitand(bb,bitcmp(ab)))
Why does the code yield error?
Let's first list the error:
Error using bitand Inputs must be signed or unsigned integers of the
same class or scalar doubles.
Error in foo (line 8)
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
Ok, so the following line yields the error:
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
We can break this down and see that both the following expressions yields errors on their own
bitand(public,negpassword)
bitand(negpublic,password)
Why? If we look at the the first of these two a bit closer, we see that public and negpassword and non-compliant for use with bitand:
public =
112 117 98 108 105 99
negpassword =
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 0 0 0 0 0
0 0 0 0 0 0 0
These two must be, at the very least, of the same dimension. See the reference for Bit-wise OR for details.
I'm not really sure what you're trying to achieve here, but not that Matlab has its own bitxor function:
public = 'public';
password = 'passwd';
if length(public)== length(password)
public = uint8(public);
password = uint8(password);
score = bitxor(public,password);
public = dec2bin(public);
password = char(password);
else
fprintf('length not ok!\n' );
end

What does for i = 1 ; i <= power ; 1 += 1) { mean?

Sorry for pretty basic question.
Just starting out. Using flowgorithm to write code that gives out a calculation of exponential numbers.
The code to do it is:
function exponential(base, power) {
var answer;
answer = 1;
var i;
for (i = 1 ; i <= power ; i+= 1) {
answer = answer * base;
}
return answer;
f
then it loops up to the number of power. And i just understand that in the flowgorithm chart but i dont understand the code for it.
what does each section of the for statement mean?
i = 1 to power, i just need help understanding how it is written? What is the 1+= 1 bit?
Thanks.
The exponential function will take in 2 parameters, base and power.
You can create this function and call (fire) it when ever it is needed like so exponential(2,4).
The for (i = 1; 1 <= power; i+=1) is somewhat of an ugly for loop.
for loops traditionaly take three parameters. The first parameter in this case i =1 is the assignment parameter, the next one 1 <= power is the valadation parameter. So if we call the function like so...exponential(2,4) is i less than 4? The next parameter is an increment/decrement parameter. but this doesnt get executed until the code inside the for loop gets executed. Once the code inside the for loop is executed then this variable i adds 1 to itself so it is now 2. This is usefull because once i is no longer less than or equal to power it will exit the for loop. So in the case of exponential(2,4) once the code inside this for loop is ran 5 times it will exit the for loop because 6 > 5.
So if we look at a variable answer, we can see that before this for loop was called answer was equal to 1. After the first iteration of this for loop answer = answer times base. In the case of exponential(2,4) then answer equals 1 times 2, now answer =2. But we have only looped through the foor loop once , and like i said a for loop goes like (assignment, validator, "code inside the foor loop". then back up to increment/decrement). So since we to loop through this for loop 5 times in the case of exponential(2,4) it will look like so.
exponential(2,4)
answer = 1 * 2
now answer = 2
answer = 2 * 2
now answer = 4
answer = 4 * 2
now answer = 8
answer = 8 * 2
now answer = 16
answer = 16 * 2
now answer = 32
So if we could say... var int ans = exponential(2,4)
Then ans would equal 32 hence, the return answer; at the last line of your code.

"/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";?

I found this line in the if.c of unix version 6.
ncom = "/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Why are there so many x's?
And why would you set this?
The code you are talking of looks like this:
ncom = "/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
while(c=nargv[0][i]) {
ncom[9+i++] = c;
}
ncom[9+i] = '\0';
All those x's act as a buffer, they are overridden by the following loop.
Therefore the code effectively adds "/usr/bin/" to the command in nargv[0].
With a little more context the code is doing this:
execv(nargv[0], nargv, np);
execv(ncom+4, nargv, np);
execv(ncom, nargv, np);
If the given command in nargv[0] is "foo" it will first try to run "foo" then "/bin/foo" and finally "/usr/bin/foo".
Be aware that above is a good example how to not do such things:
If the string in nargv[0] happens to be longer than the number of x's, the code will happily continue copying data. This will override other parts of the stack. The result is a good example of a buffer overflow. (You allocate a buffer of some size and write more data than allocated.)
This example will demonstrate the problem:
#include <stdio.h>
int main(){
char s[]="abcde";
int i;
for(i=0;i<100;i++){
printf("position %2d contains value %3d\n",i,s[i]);
s[i]=0;
}
puts(s);
return 0;
}
If you run it it will (most probably) output this:
position 0 contains value 97
position 1 contains value 98
position 2 contains value 99
position 3 contains value 100
position 4 contains value 101
position 5 contains value 0
position 6 contains value 0
position 7 contains value 0
position 8 contains value 0
position 9 contains value 0
position 10 contains value 0
position 11 contains value 0
position 12 contains value 12
position 1 contains value 0
position 2 contains value 0
position 3 contains value 0
position 4 contains value 0
position 5 contains value 0
position 6 contains value 0
position 7 contains value 0
[...]
It will fill the string (containing the ASCII values 97 to 101) with zeroes and continue writing the memory where it will find the position of the variable i it will also set it to zero. Now i is zero and therefore the loop starts again, overriding the the already overridden string again and again.
Not only local variables can be overriden, also the return address of a function might get overriden resulting in either a "segmentation fault" or execution of arbitrary code, which is often used by malware.

binary to decimal in objective-c

I want to convert the decimal number 27 into binary such a way that , first the digit 2 is converted and its binary value is placed in an array and then the digit 7 is converted and its binary number is placed in that array. what should I do?
thanks in advance
That's called binary-coded decimal. It's easiest to work right-to-left. Take the value modulo 10 (% operator in C/C++/ObjC) and put it in the array. Then integer-divide the value by 10 (/ operator in C/C++/ObjC). Continue until your value is zero. Then reverse the array if you need most-significant digit first.
If I understand your question correctly, you want to go from 27 to an array that looks like {0010, 0111}.
If you understand how base systems work (specifically the decimal system), this should be simple.
First, you find the remainder of your number when divided by 10. Your number 27 in this case would result with 7.
Then you integer divide your number by 10 and store it back in that variable. Your number 27 would result in 2.
How many times do you do this?
You do this until you have no more digits.
How many digits can you have?
Well, if you think about the number 100, it has 3 digits because the number needs to remember that one 10^2 exists in the number. On the other hand, 99 does not.
The answer to the previous question is 1 + floor of Log base 10 of the input number.
Log of 100 is 2, plus 1 is 3, which equals number of digits.
Log of 99 is a little less than 2, but flooring it is 1, plus 1 is 2.
In java it is like this:
int input = 27;
int number = 0;
int numDigits = Math.floor(Log(10, input)) + 1;
int[] digitArray = new int [numDigits];
for (int i = 0; i < numDigits; i++) {
number = input % 10;
digitArray[numDigits - i - 1] = number;
input = input / 10;
}
return digitArray;
Java doesn't have a Log function that is portable for any base (it has it for base e), but it is trivial to make a function for it.
double Log( double base, double value ) {
return Math.log(value)/Math.log(base);
}
Good luck.