How can I get Single byte from 'int' type variable? Can we
alter single bit or multiple bits in int type variable? if so,
How?

Answers were Sorted based on User's Feedback



How can I get Single byte from 'int' type variable? Can we alter single bit or multiple b..

Answer / hassan noureddine

Use bit wise unary commands:

int i = 0x5678
char LowByte = (char) i; // yield 8;

To alter the bits

i &= 0xFF; // reset upper 2 bytes
i ^= 0xFFFF // invert all bits

Is This Answer Correct ?    0 Yes 0 No

How can I get Single byte from 'int' type variable? Can we alter single bit or multiple b..

Answer / vignesh1988i

we know that integer allocates 2 bytes of memory.
to get a single byte we must type cast the integer to character using pointers.

why because,when we take int i=10;,the binary representation for 10 is 1010 or in 8 bits it can be 0000 1010.
so in memory 2 bytes will be allocated as the whole for int.

let us consider: binary 10 address (2bytes)
0000 65534
0010 65535

in the memory according to the bytes prority the binary numbers will get stored.
so , our task is to take only one byte from int.

int i=10,*j;
j=&i;
printf('%d\n",(char*)j); // type casting of ptr varables

now. in the above ex. and according to the preceeded coding it will print 0 as the output ,which is the output from only one byte of memory location (65534).

any corrections , pl. notify me


thank u

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

What is "Hungarian Notation"?

0 Answers   Celstream,


what is the purpose of the following code, and is there any problem with the code? void fn(long* p1, long* p2) { register int x = *p1; register int y = *p2; x ^= y; y ^= x; x ^= y; *p1 = x; *p2 = y; }

1 Answers   Google,


How do you initialize function pointers? Give an example?

3 Answers  


Why double pointer is used in c?

0 Answers  


WHAT IS THE DIFFERENCE BETWEEN malloc() and calloc() in c file management?

28 Answers   3D PLM, Code Studio, Deltech, IBM,


GIVEN A FLOATING POINT NUMBER HOW IS IT ACTUALLY STORED IN MEMORY ? CAN ANYONE EXPLAIN?? THE 32 BIT REPRESENTATION OF A FLOATING POINT NUMBER ALLOTS: 1 BIT-SIGN 8 BITS-EXPONENT 23 BITS-MANTISSA

0 Answers  


will u please send me the placement papers to my mail???????????????????

0 Answers  


void main() {int a[5],i,b=16; for(i=0;i<5;i++) a[i]=2*i; f(a,5,b); for(i=0;i<5;i++) printf("\n %d",a[i]); printf("\n %d",b); } f(int *x,int n,int y) { int i; for(i=0;i<n;i++) *(x+i)+=2; y=y+2; }wat r the errors in the prg.and improvise the prg to get o/p.?

2 Answers   TCS,


in b=6.6/a+(2*a+(3*c)/a*d)/(2/n); which operation will be performed first a) 6.6/a b) 2*a c) 3*c d) 2/n

1 Answers  


Program to find the absolute value of given integer using Conditional Operators

6 Answers   N Tech,


we have to use realloc only after malloc or calloc ? or we can use initially with out depending on whether we are using malloc or calloc in our program ?

2 Answers  


What is the Purpose of 'extern' keyword in a function declaration?

0 Answers  


Categories