homework help bitwise shift

This is a discussion on homework help bitwise shift within the C Programming forums, part of the General Programming Boards category; Please give me an idea how to show all the bits of a number using bitwise shift operator....and hence represent ...

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

    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, 12:22 PM
  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, 10:39 AM
  4. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 02:18 PM
  5. Bitwise Shift Operations
    By John_L in forum Tech Board
    Replies: 19
    Last Post: 02-25-2008, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21