Sunday, April 5, 2015

Java Learning , if you know C : Part-4 , Loops , Operators

Learning Java

Q > What about the extra bitwise >>> operator in Java.

To understand more about bit operators and also the diff in C and Java for them. We have to first see how they are stored in here.

Unlike C, Java doesn't have unsigned int,char or short. Everything is signed.  So first let see if there is any difference in C when bitwise operation is performed on signed /unsigned variables.

> With unsigned number, it is very easy. the number will be stored as binary representation and then output will be printed as number to bit.
  In case negative number is assigned to unsigned integer, then while storing 2's complement will be stored but only conversion back it will be treated as positive number, the MSB will
  not be treated as SIGN flag
 
> With signed number, the MSB will be treated as SIGN flag while conversion. If the number is negative it will be stored as 2's complement irrespective of signed/unsigned
Only at the time of conversion back to integer/short, it will be checked if the number was signed or unsigned. In case of signed, the MSB of bit representation will be treated as sign flag.
If it is 1, then 2's complement will be taken for number for all bits and - sign will be appended at the beginning.

********************************************************* C PROGRAM *********************************************************

1#include
#include

void bits_rep( char);
void bits_repp(unsigned char);

int main()
{
// exmaples for unsigned char
// unsigned char  a =-255; //signed char  a =-255;
// unsigned char  a =255; //signed char  a =255;
  //unsigned char  a =-1;  //signed char  a =-1;
  //unsigned char  a =256; //signed char  a =256;



int yes;int i;

do
{
printf(" enter number \n ");scanf("%d",&i);
//switch(i)
//{
//case 1:
//bits_repp(a);
printf(" signed %d  ",(char)i);
//break;
//case 2:
//bits_rep(a);
printf(" unsigned %d\n ",(unsigned char)i);
//break;
//}
printf(" Want to continue \n ");
scanf("%d",&yes);
}while(yes);

return 0;
}

// Other Bitwise operands for C  are , ~ ( act as complement, make 1 to 0 and 0 to 1), << and >> (LHS and RHS) a<<2 a="" or="">>4 , a<<2 2="" a="" bit="" by="" hand="" left="" makes="" p="" places="" shift="" side="">// ^  is XOR operator

// negative number as stored as two complement

void bits_rep( char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i));

}

void bits_repp( unsigned char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i));

}
********************************************************* C PROGRAM *********************************************************

>  Now we will see if there is any difference due to signed or unsigned in C on bitwise >> or << operators

There is no difference in unsigned or signed operator for << operator, but >> operation changes for signed and unsigned.

For unsigned the right bits get filled with 0, for signed if MSB is 1, then bits will be filled with 1.

********************************************************* C PROGRAM *********************************************************


#include
#include

void bits_rep( char);
void bits_repp(unsigned char);

int main()
{
// exmaples for unsigned char
// unsigned char  a =-255; //signed char  a =-255;
// unsigned char  a =255; //signed char  a =255;
  //unsigned char  a =-1;  //signed char  a =-1;
  //unsigned char  a =256; //signed char  a =256;



int yes;int i;

do
{
printf(" enter number \n ");scanf("%d",&i);

//switch(i)
//{
//case 1:
//bits_repp(a);
printf(" unsigned %d  ",(unsigned char)i);
//break;
//case 2:
//bits_rep(a);
printf(" signed %d\n ",( char)i);
//break;

printf(" unsigned << 2 %x  signed << 2 %d\n ",((unsigned char)i << 2) & 0xFF , (char)i << 2 );//bits_repp(i<<2 p="">
printf(" \n unsigned >> 2 %x  signed >> 2 %d\n ",((unsigned char)i >>  2) & 0xFF, (char)i >> 2 );//bits_rep(i>>2);
//}
printf("\n Want to continue \n ");
scanf("%d",&yes);
}while(yes);

return 0;
}

// Other Bitwise operands for C  are , ~ ( act as complement, make 1 to 0 and 0 to 1), << and >> (LHS and RHS) a<<2 a="" or="">>4 , a<<2 2="" a="" bit="" by="" hand="" left="" makes="" p="" places="" shift="" side="">// ^  is XOR operator

// negative number as stored as two complement

void bits_rep( char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i)&1);

}

void bits_repp( unsigned char temp)
{
int i=9;
while(i--!=1)
printf("%d ",temp>>(8-i)&1);

}
********************************************************* C PROGRAM *********************************************************

Well , what about the loops and other operators.

Hmmmm ...they work in same way as in C . So no explanation about them.

No comments:

Post a Comment