bitwise operators and bit manipulation

Sharky Forums


Results 1 to 6 of 6

Thread: bitwise operators and bit manipulation

  1. #1
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Exclamation bitwise operators and bit manipulation

    I'm an amateur coder. I've mainly just stuck with Pascal, VB, and bash shell scripts. However, I've been reading a book on C and I'm wondering if somebody could help me clarify something.

    Can somebody give me an example or a good reason for using bitwise operators and manipulating bits. I've seen some examples which are nothing more than examples on how to use the operators (&, |, >>, <<, etc.).

    Any help and or clarification would be much appreciated.

    ------------------
    root is the greed of all evil
    ## root is the greed of all evil ##

  2. #2
    Reef Shark
    Join Date
    Apr 2001
    Location
    Austin, TX
    Posts
    371

    Post

    See the sig :P

    No, seriously, the reason is because you can't have anything smaller than a byte. If you wanted to manipulate that byte, you need to use the bitwise operators.

    Let's take the old-sk00l DWORD return code for an example. If you return:

    11011000000000011111111000110101

    Each one of those 32 bits can stand for something. You can check these values, and manipulate them by shifting, and XOR'ing them to death.

    Now, I would suggest using a struct or a class or something. Most of the times that I've seen this used is to manipulate/check data that has been returned in this form that somebody has no control over (eg: some of the PalmOS API). Another good use is cryptography, however I can't really give a good example of this. Just hit Google.

    ------------------
    Advocate of the Sharky (Ultra) High-Resolution Club [SHRC]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}
    Advocate of the Sharky (Ultra) High-Resolution Club [SHRC]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

  3. #3
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Question

    Thank you for the reply.

    Do you (or anybody else for that matter) use bitwise operators alot? It seems like you would only use them for special cases like you said (Crypto and special API programming).

    Is there anytime where you regularly use bitwise operators?

    Any help would be very much appreciated.





    ------------------
    root is the greed of all evil
    ## root is the greed of all evil ##

  4. #4
    Reef Shark gammaray51's Avatar
    Join Date
    Sep 2000
    Location
    Virgina Beach, VA
    Posts
    493

    Post

    Originally posted by biosx:
    Thank you for the reply.

    Do you (or anybody else for that matter) use bitwise operators alot? It seems like you would only use them for special cases like you said (Crypto and special API programming).

    Is there anytime where you regularly use bitwise operators?

    Any help would be very much appreciated.



    Ive done some embedded control programming with small microprocessors that require bitwise operators. Mostly the operators are used for masking.

    An example, which I used a lot, was when I started programing for the 68HC11. It has 4 ports and some of those can be both input and output ports (input denoted as a 0, output as 1). This was stored in DDRD (data data direction port D). If it was half in, half out then it would be:

    DDRC = 00001111.

    If you need to check which ports were in and which were out you could use the bitwise and to mask out all the other bits.

    The main reason I can think of for the &gt;&gt; &lt;&lt; operators is for quick divide and multiply by 2.

  5. #5
    Reef Shark
    Join Date
    Feb 2001
    Location
    Redmond, Wa, USA
    Posts
    315

    Post

    I game coding it's used A LOT...and I do mean a LOT.

    For one

    Shifting a bit down one takes a lot less cycles than dividing a value by 2.
    ie

    2&gt;&gt;1 = 1

    Secondly, for using with flags. On console machines with limited memory you can store 32 boolean values in one U32 value instead of using a seperate u32 for each boolean value (as MSC tends to default too).

    When doing compression or binary search trees you use a lot of bit comparisons/bit shifting.

    digital controllers on the PC usually return all of thier values in a u32 value. So each button pressed would be a different bit in the return valued.

    There are lots and lots of reasons to learn/use bitwise operations.



    ------------------
    P4 1.4 oc'd to 1.53 (fsb 112 mhz)
    Asus P4t motherboard
    512mb pc800 memory
    Elsa Gladiac 920 GeForce 3 (240 gpu, 540 mem)
    Creative Sound Blaster Live Platinum 5.1
    2 30 gig IBM deskstar drives
    Promise FastTrack 100 IDE RAID controller
    Sony G400 19" monitor

    second machine:
    Athlon 1.33ghz 266 mhz fsb
    GeForce 2 Ultra
    512 mb DDR 266 ram (samsung)
    Creative labs Sound Blaster Live value
    IBM deskstar 30gig
    IBM deskstar 15gig
    19" Hitachi Flat screen (CM771)
    Athlon FX 53
    Asus SK8V motherboard
    2 gigabyte DDR 400 ECC RAM
    ATI Radeon x800 Pro
    Creative Sound Blaster AUDIGY 2 zs
    2 74gb WD Raptor Drives(Raid 0)
    16x DVD
    Logitech 5.1
    ThermalTake Case
    Viewsonic G220fb

  6. #6
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Smile

    Thanks for your replies, I think I understand a little better now.

    Thanks again.

    ------------------
    ## root is the greed of all evil ##

    /* Navi Specs */
    Abit KT7-RAID
    Duron 800 @ 1GHz
    Geforce 2 GTS
    512MB Micron/Crucial SDRAM
    SB Live! 5.1 w/ Live! Drive
    Maxtor ATA100 40GB
    Pioneer 16x DVD
    HP 9300 series CD-RW
    Netgear FA311 NIC
    ## root is the greed of all evil ##

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •