Hewlett Packard Interview Question for Software Engineer / Developers


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;
}

- aka[1] October 17, 2015 | Flag Reply
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;

	int mask = 1;	
	while (--count > 0)
		mask = (mask << 1) | 1;

	return number ^ (mask << start);
}

- IC October 24, 2015 | Flag Reply
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

- Abhi October 17, 2015 | Flag Reply
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;
	int mask = 01, mask1 = 01, new_mask = 0, i = 0;
	
	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--) {
		if (mynum & (mask<<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++) 
		new_mask ^= (mask1 << j);
	printf("%d\n",new_mask);
	
	for (j = (BITBYTES*sizeof(int)-1); j >= 0; j--) {
		if ((mynum & (mask << j))^(new_mask & (mask << j)))
			putchar('1'); 
		else 
			putchar('0');
	}
	mynum ^= new_mask;
	printf("\nThe modified number is: %d\n",mynum);
	system("pause");
}

- Anonymous October 25, 2015 | Flag Reply
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 } - lo October 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int toggleBits( int A, int start_bit, int numBits) { 
	int mask; mask = (1<<numBits)-1; 	// mask with numBits 1's in lower bits 
	return (A ^ (mask << (start_bit -1)); // shift mask, then XOR

- Anonymous October 29, 2015 | Flag
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;
}

- apoorva December 23, 2015 | Flag Reply
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");
}

- Sandeep Chauhan December 29, 2015 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More