## Hewlett Packard Interview Question for Software Engineer / Developers

• 1
of 1 vote

Team: Networking
Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

#include <stdio.h>

#define toggle(a, b, c) \
a^(((1<<b)-1)<<c)
int main(void) {
int a=34;
a = toggle(a, 4, 3);
printf("%x", a);
return 0;
}

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````public static int Toggle(int number, int start, int count)
{
if ((start + count) > 31)
throw new IndexOutOfRangeException();
if (count == 0) return number;

while (--count > 0)

return number ^ (mask << start);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a new bit vector with same length. Set bits from start to end point as opposites to the main vector and do an AND for these two. Use the results as the toggled vector

Comment hidden because of low score. Click to expand.
0
of 0 vote

This code snippet appears long as it prints out the original and modified bit streams.

``````#include <stdio.h>
#include <stdlib.h>
#define BITBYTES 8

int main(void)
{
int start_bit = 5, num_of_bits = 15, mynum, j;

printf("Enter number you wish to flip bits for:\t");
scanf("%d",&mynum);
printf("\n");

printf("Printing bits in number enetered as input\n");
for (j = (BITBYTES*sizeof(int)-1); j >= 0; j--) {
putchar('1');
else
putchar('0');
}
printf("\n");

printf("Modified mask stream is shown below:\n");
for (j = start_bit; j < ((num_of_bits - start_bit) + start_bit); j++)

for (j = (BITBYTES*sizeof(int)-1); j >= 0; j--) {
putchar('1');
else
putchar('0');
}
printf("\nThe modified number is: %d\n",mynum);
system("pause");
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{int toggleBits( int A, int start_bit, int numBits) { int mask; mask = (1<<numBits)-1; // mask containing numBits 1's in lower bits return (A ^ (mask << (start_bit -1)); // shift the mask to the correct position, then XOR }
Comment hidden because of low score. Click to expand.
0

``````int toggleBits( int A, int start_bit, int numBits) {
return (A ^ (mask << (start_bit -1)); // shift mask, then XOR``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<stdio.h>
int main()
{
int a[50],start,no_of_pos,i,n;
printf("enter length of number");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("string is:");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("enter starting position and length of number u want to toggle");
scanf("%d%d",&start,&no_of_pos);
for(i=n-start;i>=n-(start+no_of_pos);i--)
{
if(a[i]==1)
a[i]=0;
else
a[i]=1;
}

printf("new string is:");
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
int data, start, number, power, sum=0;
printf("enter any data");
scanf("%d", &data);
printf("you entered is\n");
convertD2B(data);
printf("enter the start bit and number of bits");
scanf("%d %d", &start, &number);
for(power=start; power<start+number; power++)
{
sum=sum+pow(2, power);
}

data=data^sum;
printf("\nthe modified data is \n");
convertD2B(data);
return 0;
}

void convertD2B(int n)
{
int c, k;
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.