Thread: homework help bitwise shift

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    Lightbulb homework help bitwise shift

    Please give me an idea how to show all the bits of a number using bitwise shift operator....and hence represent the number in 2's complement representation

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You can use bitwise-and like so to get the least significant bit:

    Code:
    int myNumber;
    int myBit = myNumber & 1;
    You can then do a right-shift and repeat the process to get the next bit, and so on, until all bits have been extracted (you could save these in an array if you want?). It might also be a good idea to look at the sizeof() operator, if you want to know how many bytes/bits are in a given type.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logical shift and arithmetic shift.(bit shifting in C )
    By A34Chris in forum C Programming
    Replies: 20
    Last Post: 09-03-2012, 11:22 AM
  2. Cycle shift with bitwise operators, help pls
    By kevinstrijbos in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 12:15 PM
  3. Bitwise Shift
    By blurx in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 09:39 AM
  4. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 01:18 PM
  5. Bitwise Shift Operations
    By John_L in forum Tech Board
    Replies: 19
    Last Post: 02-25-2008, 11:22 AM