Thread: Bitwise Operators in C

  1. #1
    Registered User
    Join Date
    Nov 2020

    Bitwise Operators in C

    Can you help me with this example where i take from the deitels book.
    In the example we compare the value and the displayMask value with & and we are writing 1 or 0 but the point that i dont understand that how do we compare this two value cause there is more than one value but we are writing only one bit for each loop.
    thanks in advance.
    // Fig. 10.7: fig10_07.c
    // Displaying an unsigned int in bits
    #include <stdio.h>
    void displayBits(unsigned int value); // prototype
    int main(void)
        unsigned int x; // variable to hold user input
    printf("%s", "Enter a nonnegative int: ");
        scanf("%u", &x);
    // display bits of an unsigned int value
    void displayBits(unsigned int value)
        // define displayMask and left shift 31 bits
    unsigned int displayMask = 1 << 31;
        printf("%u = ", value);
        // loop through bits
    for (unsigned int c = 1; c <= 32; ++c) {
            putchar(value & displayMask ? '1' : '0');
            value <<= 1; // shift value left by 1
    if (c % 8 == 0) { // output space after 8 bits
    putchar(' ');

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Please edit your code.
    Either "copy as text" from your IDE/Text editor, or "paste as text" in your browser (or try ctrl-shift-v).

    I edited it here for you, but you need to do this yourself.
    %n specifier use in C
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators in C.
    By RyanC in forum C Programming
    Replies: 1
    Last Post: 02-19-2016, 06:00 PM
  2. bitwise operators
    By roelof in forum C Programming
    Replies: 22
    Last Post: 05-18-2011, 11:53 AM
  3. bitwise operators??!?
    By Mr_Jack in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2004, 08:05 AM
  4. Bitwise operators
    By Unregistered in forum C Programming
    Replies: 22
    Last Post: 03-24-2004, 11:03 AM
  5. bitwise operators
    By Qasim in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 09:23 AM

Tags for this Thread