Thread: Assignment Help

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    1

    Assignment Help

    Hello, I'm a newbie in c programming. Can someone help me on this assignment. The code is what I have done so far: part_2.c



    PART2: Bit Masking
    (3 marks)

    1. Initialize an 8-bit binary number (call it num1) that equals the last digit of your student#.
    2. Initialize a binary mask (call it mask1) that equals the next digit (before the last) of your student#.
    3. Your code should implement the following:


    1. OR mask1 with num1, print the result
    2. toggle num1, print the result

    To check part (a): use num1=0b00000101 and mask1= 0b00000010. The result is 7
    To check part (b): for the same num1, result is -6

    1. In the space below, attach a screenshot of the code and the results window.



    Code:
    #include<stdio.h>
    void bit_mask(unsigned char x) {
        unsigned char m = 0x80;
        for ( ; m; m >>= 1){
            putchar(x & m ? '1' : '0');	
    	}	
        putchar('\n');
    }
    
    int part_2() {
        char a[] = "4";
        unsigned char b[4];
    	size_t i;
    	for (i = 0; i < 2; i++){
        	b[i] = a[i] - '0';
    	}
        for (i = 0; i < 2; i++){
            bit_mask(b[i]);	
    	}	
        return 0;
    }
    Last edited by Salem; 12-03-2020 at 11:28 AM. Reason: Inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > OR mask1 with num1, print the result
    > toggle num1, print the result
    None of your code does either of these things.

    You might save some confusion by renaming your 'bit_mask' to something better, like print_in_binary.
    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. Help for my assignment!
    By 110abbidi in forum C Programming
    Replies: 5
    Last Post: 08-09-2012, 11:25 PM
  2. Help with assignment !!
    By CruelSoulz in forum C Programming
    Replies: 2
    Last Post: 07-02-2012, 07:06 AM
  3. Need help on an assignment. TIA
    By Ikki de Vera in forum C Programming
    Replies: 1
    Last Post: 03-14-2012, 09:05 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. Assignment help
    By the_winky_files in forum C Programming
    Replies: 19
    Last Post: 10-02-2005, 04:14 PM

Tags for this Thread