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 has to put when we are inserting as assembly language code into the C code? or When we are inserting as assembly language code into the C code we have to insert one thing at the start and of the assembly language. What are they?

2 Answers  


i have a written test in tomorrow

1 Answers   NNN, Value Labs,


program for comparing 2 strings without strcmp()

4 Answers  


Who invented b language?

0 Answers  


which will return integer? a) int*s ( ) b) ( int* ) s( ) c) int ( *s ) ( )

1 Answers   C DAC,






int main() { int i=-1,j=-1;k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d%d%d%d%d",i,j,k,l,m); }

3 Answers   HCL,


What are run-time errors?

0 Answers  


How can I read data from data files with particular formats?

0 Answers  


The statement, int(*x[]) () what does in indicate?

0 Answers  


What does == mean in texting?

0 Answers  


Why void is used in c?

0 Answers  


main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); }

3 Answers   ME,


Categories