Thread: Understanding problem with task: "Testing bits of integer"

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    6

    Understanding problem with task: "Testing bits of integer"

    Hey, I am trying to understand the task, but I do not really get it.
    I shall write a program that prompts for an integer number and stores it into a variable of data type int. The program should test all bits of this variable by using C langauge bit operators and print the result. (1 = bit set, 0 = bit not set) to stdout. The program must work correctly for any length of the int data type(by using sizeof()).

    Tbh I do not really understand the meaning of it. Does it mean I should do a decimal-to-binary conversion by using C language bit operators?

    What means "test all bits of the read integer variable"? I means "test" in this case? What should you test?

    I do not want you to solve this problem, but maybe you know what this task could mean.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    For example, if you typed in the number 5, you would expect

    bit 2 is set
    bit 1 is clear
    bit 0 is set

    You test the least significant bit with
    if ( (value & 1) == 1 )

    You can shift bits around with say
    value <<= 1
    or
    value >>= 1

    You get the number of bits by doing say
    sizeof(value) * CHAR_BIT
    CHAR_BIT is defined in the header file limits.h.
    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. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 4
    Last Post: 08-16-2014, 02:35 AM
  3. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  4. Problem with "pointer from integer without a cast"
    By firyace in forum C Programming
    Replies: 6
    Last Post: 05-11-2007, 09:48 AM
  5. problem with "comparison between pointer and integer"
    By unregistered in forum C Programming
    Replies: 8
    Last Post: 05-24-2002, 11:49 PM

Tags for this Thread