Thread: C Bit Manipulation

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    C Bit Manipulation

    Hello fellow programmers,

    I am new to the cprogramming forum and am requesting some assistance in further understanding bit manipulation in c.

    I am undertaking my first attempt at learning c, as i have previously learnt c++.

    I was confronted with a question regarding bit manipulation and using certain bits within a bit field to store values.

    The question i was asked is that a card is made up of 7 bits.

    2 bits = suit (Diamond, Clubs, Hearts, Spades)
    4 bits = value (A,K,Q,J,10-2)
    1 bit = colour (Black, Red)

    Can someone please assist me in clearing up my understanding of bit operations ( bitwise &, bitwise OR,<< ,>>) and also provide an example of how the operations are carried out.

    Thankyou fellow programmers

    Sicilian_10

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A very timely thread:
    stuck on card game in C. any suggestions?

    Here's what I'd like to see - post up something that has you stuck, or gives you the wrong answer, and let's deal with actual code.

    A bit manipulation tutorial lies outside the bounds of a post on a forum, since there are already many such tutorials on the web (Google is your friend), and of course, in nearly every book as well.

    Our time is limited, please be as specific as possible with your problem.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if your byte contains following bits:
    RSSVVVVC
    R - reserved
    S - suit
    V - value
    C - color

    you'll do something like

    Code:
    unsigned char card = 0x??;
    int suit = (card >> 5) & 0x03 ;
    to retrieve suit - first shift right to get rid of the bits from the left and create byte like

    00000RSS

    and than - mask result to get rid of bits to the right

    000000SS

    To set the suit do the opposite
    Code:
    unsigned char suit = 3;
    
    card |= (suit << 5);
    First shift suit to the left to the correct bit position

    0SS00000

    Then use bit-wise OR to set the correct bits... (I suppose that you start with zero-bit sequence)

    To clear corresponding bits use something similar:

    Code:
    unsigned char card = ...;
    
    card &= ~(0x3 << 5);
    Take the mask for the fied you need to clear
    00000011

    shift it to correct bit position

    01100000

    Invert it

    10011111

    And use the Bit-wise AND to clear the corresponding bits
    Last edited by vart; 03-19-2010 at 12:59 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > A very timely thread:
    Yes, except this one is a hell of a lot easier to map the suit into the colour
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    To me bitpacking makes for a clearer picture but YMMV:
    Packing Bits with Bit Fields
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM

Tags for this Thread