Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please explain me this code (Bitwise problem)

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Bitwise problem

Hi

I need your help to understand the code:

"printf("%d", ( byte & 128 ) ? 1 : 0 );"

*********************************************************************** *
void binary_op(char byte); /* Print a byte in binary. */

/************************************************************************/

main()
{
char byte=55;

binary_op(byte); /* Print a byte in binary. */
}

/************************************************************************/

void binary_op(char byte)
{
int count=8; /* Number of bits in a byte. */

while(count--)
{
/* AND the high order bit (the
* left one) If the bit is set,
* print a ONE. */
printf("%d", ( byte & 128 ) ? 1 : 0 );

/* Move all the bits LEFT. */
byte <<= 1;
}

printf("\n");
}
 

Re: Bitwise problem

"printf("%d", ( byte & 128 ) ? 1 : 0 );"

I think it is the short for:

Code:
if (byte & 128) {
   printf("%d", 1);
} else {
   printf("%d", 0);
}

And your function binary_op() is printing byte in binary form.
 

Bitwise problem

The statement is checking whether (only) the "MSB" of "BYTE" is set or not. If it's set then print "1" else print "0"
 

Bitwise problem

A more compact syntax:
printf("%d", !!(byte & 128));
 

Re: Bitwise problem

Ok....why use 128?

Maverickmax
 

Re: Bitwise problem

Ok....why use 128?

Because it is highest bit in the byte:

128d = 80h = 10000000b

128d (or simply 128) is decimal notation, 80h - hexadecimal, 10000000b - binary.
 

Re: Bitwise problem

it send the state of the 8th bit of the char byte through the serial port .


good luck
 

Re: Bitwise problem

I admit that I don't fully understand how this algorithm works because I don't know why '(55 & 128)' then shift to left......

Is there any simple algorithm that convert decimal to binary


Maverickmax
 

Re: Bitwise problem

I admit that I don't fully understand how this algorithm works because I don't know why '(55 & 128)' then shift to left

You are thinking in decimal form, as you has 10 fingers at your hands. You must understand, that processor is thinking in binary form as he has only one finger at hands :). So, you see, there is decimal notation in your program especally for you. But compiller is converting every number to binary form preparing it to processor.

Your example is (55 & 128) in your point of view.
Ok, now look to your program with processor point of view:
(00110111b & 10000000b) = 0
next shift 55 to the left:
(01101110b & 10000000b) = 0
shift again:
(11011100b & 10000000b) = 1

Continue this exercise yourself and compare result column with 55 ...

You see, this way processor is changing your 55 to your 00110111 at your display.

That is simplest algorithm to convert any number to binary point of view.

Is there any simple algorithm that convert decimal to binary?

You will get reply to your question in your own program. That is reply for processor.

But algorithm will be another if you will try to convert numbers by yourself.

Look:

Code:
binary:
X   X   X    X    X    X    X    X 
128 64  32   16   8    4    2    1

So, add all numbers where X=1 and you will convert binary to decimal. And vice versa, decompile decimal to this powers of 2 and you will get binary from decimal.

Example:

55 = 0 + 0 + 32 + 16 + 0 + 4 + 2 + 1

or 00110111b

Do you get it?
 

    Maverickmax

    Points: 2
    Helpful Answer Positive Rating
Re: Bitwise problem

Aye I get it now. Thank you

If i use '555', 2048 will be used instead of 128 because each number represents nibble.

Is it correct?

Maverickmax
 

Re: Bitwise problem

If i use '555', 2048 will be used instead of 128 because each number represents nibble.

Yes and No - it depends of program that you will make. You can operate with any bit that is higher or equal with highes possible bit of your variable.

We were talking about byte. You know byte consists of 8 bits. As a rule, processor is operating with bytes. 2 bytes = 16 bits, and highest one will be 32768.

You see, that is not so suitable to think in decimal system, so most programmers are thinking in hexadecimal system. It is much easer.

Look:
128 = 80h

Nibble is a half of a byte. So, there are 2 nibbles in byte. Hexadecimal number consists of two nibbles: 8 and 0 here.

There are 4 bits in nibble and to convert quickly you have to keep in mind only 4 numbers 1, 2, 4, 8.

So:
80h = (8+0+0+0) (0+0+0+0)=10000000b

71h = (0+4+2+1) (0+0+0+1)=01110001b

35h = (0+0+2+1) (0+4+0+1)=00110101b

7Bh = (0+4+2+1) (8+0+2+1)=01111011b

You see, it is very easy to convert from hex to bin and vica versa. So, try to think in hexadecimal system.

P.S. Hex system number sequence is: 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11, ... ,1D,1E,1F,20, ... ,FD,FE,FF,100,101, ...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top