Adobe Interview Question
Member Technical StaffsCountry: India
Interview Type: In-Person
wht abt this code??
int p;
printf("enter any hex number");
scanf("%x",&p);
int q;
int r;
q=p;
r=q<<8;
p=p|r;
r=q<<16;
p=p|r;
r=q<<24;
p=p|r;
printf("0X %x",p);
I don't understand why you get 8 votes, I would give you -1 if I have time.
int converint(int a)
{
return a<<24|a<<16|a<<8|a;
}
@Downvote.if.I.had.time.Anonymous:
He first computes 0x2525 and then shifts that and ORs that. Yes, there is a typo of using n instead of x, but we can give him that.
But your solution is more readable, and quite possibly more efficient (inspite of the extra bit shift and bitwise or as compared to the +8 solution), as the compiler can work with registers only and not deal with any locals.
Corrected version
int convert(int x)
{
int c = x;
c = c << 8 | c;
c = c << 16 | c;
return c;
}
Correct Me if I m Wrong :
int convert(int x)
{
int c = x;
c = c << 8 | c;
c = c << 8 | c; // Rather Than c = c << 16 | c;
return c;
}
after using left shift operator by 8(0x25<<8),only last 8 bit information will still there(0x00) not any 16 bit information like(0x2500).
main()
{
int i;
int count;
long y;
while(1)
{
y=0;
count=0;
printf("Input and number :");
scanf("%u", &i);
printf("You entered %d (0x%x)\n", i,i);
if(i>255)
{
printf("enter number less that 256\n");
continue;
}
while(count++!=sizeof(y))
{
printf(".");
y = (y<<8)|i;
}
printf("\nOutput %ld (0x%lx)\n", y,y);
}
main()
{
int i;
int count;
long y;
while(1)
{
y=0;
count=0;
printf("Input and number :");
scanf("%u", &i);
printf("You entered %d (0x%x)\n", i,i);
if(i>255)
{
printf("enter number less that 256\n");
continue;
}
while(count++!=sizeof(y))
{
printf(".");
y = (y<<8)|i;
}
printf("\nOutput %ld (0x%lx)\n", y,y);
}
This was my answer. He was satisfied with this.
- Nitin Gupta November 03, 2012