“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
i believe those answer are not what they expected , the question actually ask you to use bitwise operator such as ^,&, to swap these two integer, think about it, i will give the answer later
Doing one left shif
x = 00000110 …which is 6
second left shift x = 00001100….which is 12
third left shift x = 00011000….which is 24
fourth (and last) left shift x = 00110000 which is 48
48 is the result..which equal to (x * 16) = (3 * 16)
ROFL, you’re all horrible programmers. only like two people got this question. the answer has to do with bit shifting, as its clearly hinted by the base2 multiplication number (16)
no one will hire you if you give that silly +- thing
a=a+b;
b=a-b;
a=a-b;
assume variables are a and b
a becomes (a XOR b)
b becomes (b XOR a)
a becomes (a XOR b)
do :
a=a+b-a
b=b+a-b
the previous answer is incorrect.
Consider this:
a = 2, b = 3
a = a + b - a;
3 2 3 2
b = b + a - b;
3 3 3 3
result
a = 3, b=3
a=a+b
b=a-b
a=a-b
will do it withoutextra memory
a = 8; b = 9;
a = a + b; 17 = 8 + 9;
b = a - b; 8 = 17 - 9;
a = a - b; 9 = 17 - 8;
NONONONO..
there’s a very easy way to do this…
a^=b^=a^=b
which is
a = a xor b
b = b xor a
a = a xor b
Why does not the following code report integer overflow?
#include <stdio.h>
#include <limits.h>
void main() {
int a = 1, b = INT_MAX;
a = a + b; // <— ?
b = a - b;
a = a - b;
printf(”a = %d, b = %d”, a, b);
}
i believe those answer are not what they expected , the question actually ask you to use bitwise operator such as ^,&, to swap these two integer, think about it, i will give the answer later
int main (void)
{
int a = 5;
int b = 7;
a = a + b - (b = a + b - b);
cout << “a = ” << a << endl;
cout << “b = ” << b << endl;
return 0;
}
This is one more solution 4 it.
a = a * b;
b = a / b;
a = a / b;
or u can write this in one line as
a=a*b/(b=a);
bvreddy - what if b=0???
The question asks how to multiply a variable by 16. I would just shift it left 4 times.
x = x
Where did the shift operator go? Let me try it again.
x = x <<4;
Assume the Variable is X = 5 and has to be multiplied by Variable Y = 16 [ The will always be the same for any scenario ]
int main (void)
{
int x = 5;
int y = 16;
int answer = 0;
for (int i = 0; i
int main (void)
{ int x = 5; int y = 16; int answer = 0;
for (int i = 0; i
Shift its bits 4 to the left.
Add the variable to itself 16 times. The result would be same as var*16.
Hey that’s simple,
Just left shift the variable 4 times are you will get the answer!
int a = 5;
printf(”%d”, a
just we add the variable 16 times to itself using loop
that is
int a=5;
int b=16;
for(int i=1;i
I think that the shift operator suits best here.
just do 4 left shifts
if x is the number then
x in binary x = 00000011
Doing one left shif
x = 00000110 …which is 6
second left shift x = 00001100….which is 12
third left shift x = 00011000….which is 24
fourth (and last) left shift x = 00110000 which is 48
48 is the result..which equal to (x * 16) = (3 * 16)
Regards
this editor is dumb too
x = 3, before doing anything
Just shift 4 bits to left.
Or divide by 0.0625.
assume the variable is a
int main(void)
{
int a = 6; //anyvalue can be used
int sum = 0;
for (i =0; i
There is a very simple trick in C:
// a is the int. to be multiplied x16
// a16 will hold the result
int a16 = 4
just do bit shifts…
x= x
sorry…my previous post has something wrong in it..
it’s “x=x
ROFL, you’re all horrible programmers. only like two people got this question. the answer has to do with bit shifting, as its clearly hinted by the base2 multiplication number (16)
no one will hire you if you give that silly +- thing
Do a left shift 4 times, i.e. x
Guys! Why don’t you think simple..
Just divide the variable by 1/16
a / (1/16)
a/0.0625
binary format of 5 is
x in binary x = 00000101
And it is not 00000011.
Hey Guys! Understand the problem as it is. Do not think much. Do simply:
somevariable = x;
x
suppose variable is A
now to multiply it by 16 do the following
for(i=0;i
Left Shift the variable 4 times.
#include
int main(void)
{
int n,i;
int mul=0;
printf(”\nEnter the number to be multiplied by 16: “);
scanf(”%d”, &n);
for(i=0;i
If I need to multiply a small integer value by 16 in C/C++
answer = var
Divide by 1/16.
Answer = Varibale / (1/16)
Shift it left by 4 bits
Or
Add the number 16 times
main()
{
long int i;
printf ( “\n Enter the number ” ) ;
scanf( “%ld”,&i);
i=i
int x,i;
int a=0;
for i = 1 to 16
{
a=x+a;
}
print a;
Leave an Answer/Comment