# print out a decimal value ( unsigned int a ) to binary value from MSB to LSB. - Linux

This is a discussion on print out a decimal value ( unsigned int a ) to binary value from MSB to LSB. - Linux ; hi, l like to print out a decimal value ( unsigned int a ) to binary value from MSB to LSB. at the following code , when I run it I can see "0 0 0 0 0 0 0 ...

# Thread: print out a decimal value ( unsigned int a ) to binary value from MSB to LSB.

1. ## print out a decimal value ( unsigned int a ) to binary value from MSB to LSB.

hi,

l like to print out a decimal value ( unsigned int a ) to binary value from
MSB to LSB.

at the following code , when I run it I can see "0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2147483648 0 -2147483648 0 "

why I got the value of -2147483648? , I think it should be 1.
What's wrong?

==================================
#include

int main ()
{

unsigned int a = 10;
int i = 0;
unsigned int val = 0;
int bit = 0;

printf ( "size of int = %d\n", sizeof ( a ) );

bit = sizeof (a) * 8;

while ( i < bit )
{
val = ( a << i++ ) & 0x80000000;
printf ( "%d ", val );
}

printf ( "\n" );

return 0;
}
~
~

Thansk.

2. ## Re: print out a decimal value ( unsigned int a ) to binary value from MSB to LSB.

On a sunny day (Mon, 18 Jun 2007 20:54:32 +0900) it happened "google-rambo88"
wrote in :

>hi,
>
>l like to print out a decimal value ( unsigned int a ) to binary value from
>MSB to LSB.
>
>at the following code , when I run it I can see "0 0 0 0 0 0 0 0 0 0 0 0
>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2147483648 0 -2147483648 0 "
>
>why I got the value of -2147483648? , I think it should be 1.
>What's wrong?
>
>
>
>==================================
>#include
>
>int main ()
>{
>
> unsigned int a = 10;
> int i = 0;
> unsigned int val = 0;
> int bit = 0;
>
> printf ( "size of int = %d\n", sizeof ( a ) );
>
> bit = sizeof (a) * 8;
>
> while ( i < bit )
> {
> val = ( a << i++ ) & 0x80000000;
> printf ( "%d ", val );
> }
>
> printf ( "\n" );
>
> return 0;
>}

Don't you think 0x80000000 is a bit BIG?
#include
#include

main(int argc, char **argv)
{
uint a;
int i, b, c;

a = atoi(argv[1]);
fprintf(stderr, "a=%d\n", a);

b = sizeof(a);
fprintf(stderr, "bits=%d\n", b * 8);

for(i = (b * 8) - 1; i >= 0; i--)
{
c = (1 << i);

if(a & c) printf("1");
else printf("0");

}

printf("\n");
}

# ./test 256
a=256
bits=32
00000000000000000000000100000000

3. ## Re: print out a decimal value ( unsigned int a ) to binary valuefrom MSB to LSB.

> hi,
>
> l like to print out a decimal value ( unsigned int a ) to binary value from
> MSB to LSB.
>
> at the following code , when I run it I can see "0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2147483648 0 -2147483648 0 "
>
> why I got the value of -2147483648? , I think it should be 1.
> What's wrong?

[...]

> val = ( a << i++ ) & 0x80000000;
> printf ( "%d ", val );

0x80000000 is hardly ever 1 (that is 0x00000001).

#include

int main ()
{
unsigned int a = 10;
int bits = sizeof(a) * 8;
int i;
for(i = bits - 1; i >= 0; --i)
printf("%d", (a >> i) & 1);
printf( "\n");
return 0;
}

-- Sergei.

4. ## Re: print out a decimal value ( unsigned int a ) to binary value from MSB to LSB.

On Jun 18, 8:45 am, Sergei Organov wrote:
> > hi,

>
> > l like to print out a decimal value ( unsigned int a ) to binary value from
> > MSB to LSB.

>
> > at the following code , when I run it I can see "0 0 0 0 0 0 0 0 0 0 0 0
> > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2147483648 0 -2147483648 0 "

>
> > why I got the value of -2147483648? , I think it should be 1.
> > What's wrong?

>
> [...]
>
> > val = ( a << i++ ) & 0x80000000;
> > printf ( "%d ", val );

>
> 0x80000000 is hardly ever 1 (that is 0x00000001).
>
>
> #include

#include

> int main ()
> {
> unsigned int a = 10;
> int bits = sizeof(a) * 8;

Correction:
int bits = sizeof(a) * CHAR_BIT;

> int i;
> for(i = bits - 1; i >= 0; --i)
> printf("%d", (a >> i) & 1);
> printf( "\n");
> return 0;
>
> }
>
> -- Sergei.

5. ## Re: print out a decimal value ( unsigned int a ) to binary valuefrom MSB to LSB.

Sergei Organov wrote:
>
>
>>hi,
>>
>>l like to print out a decimal value ( unsigned int a ) to binary valuefrom
>>MSB to LSB.
>>
>>at the following code , when I run it I can see "0 0 0 0 0 0 0 0 0 0 0 0
>>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2147483648 0 -2147483648 0 "
>>
>>why I got the value of -2147483648? , I think it should be 1.
>>What's wrong?

>
>
> [...]
>
>
>> val = ( a << i++ ) & 0x80000000;
>> printf ( "%d ", val );

>
>
> 0x80000000 is hardly ever 1 (that is 0x00000001).
>
>
> #include
>
> int main ()
> {
> unsigned int a = 10;
> int bits = sizeof(a) * 8;
> int i;
> for(i = bits - 1; i >= 0; --i)
> printf("%d", (a >> i) & 1);
> printf( "\n");
> return 0;
> }
>
> -- Sergei.

Or just replace

printf ( "%d ", val );
by
printf ( "%d ", val ? 1 : 0 );
or
printf ( "%d ", !!val );

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef MÃ¶llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html