newbie seeks insight on bitwise operations..

From: Alan Holloway (alanh_at_kiaro.net)
Date: 07/28/04


Date: 28 Jul 2004 08:09:30 -0700

Hi all,

I'm currently learning C (cliche' entry?) and I am finding bitwise
operations a tad challenging. I have a sound understanding of how
boolean logic works (i think), but I find it hard to evaluate the
constructs when I see them in code.

For example, in a network program, a port number is subjected to the
following operation:

port = (port & 0xff) << 8 | port >>8;

AFAIK, port is being assigned the outcome of :

1) An AND operation to ensure that the variable 'port' is not greater
than 255.
2) A left shift by 8 bits (not too sure why though)
3) An OR with the outcome of a right shift by 8 bits (again, not too
sure why).

My apologies for my obvious lack of clue, but i'd really appreciate
some help here in understanding,
as this has been a bit of a show stopper for sometime, and i've
finally realised i'm probably not going to crack this on my own :)

Thanks all

Al.



Relevant Pages

  • Re: newbie seeks insight on bitwise operations..
    ... > operations a tad challenging. ... I have a sound understanding of how ... On a system where CHAR_BIT is 8 and port is a 2 byte integer, ... port & 0xff zeroes the most significant byte. ...
    (comp.lang.c)
  • Re: newbie seeks insight on bitwise operations..
    ... > I'm currently learning C and I am finding bitwise ... > operations a tad challenging. ... I have a sound understanding of how ... > For example, in a network program, a port number is subjected to the ...
    (comp.lang.c)
  • Re: newbie seeks insight on bitwise operations..
    ... > I'm currently learning C and I am finding bitwise ... > operations a tad challenging. ... I have a sound understanding of how ... > For example, in a network program, a port number is subjected to the ...
    (comp.lang.c)
  • Re: newbie seeks insight on bitwise operations..
    ... I have a sound understanding of how ... > then that will swap the bytes in port. ... still say that it swaps the two low-order octets, ...
    (comp.lang.c)