4 Replies - 1633 Views - Last Post: 05 October 2014 - 01:39 PM Rate Topic: -----

#1 kristian1986   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 14

Really need help. bits and bytes

Posted 05 October 2014 - 04:37 AM

I really need help.
I am new to C-code and take a hard class in c-code. some days a go I got this exercise I have to deliver in a couple of days, but i dont understand it.
Can somone help med understand it?.

As far as I understand I have 3 files. The main.c, the bitman.c and the bitman.h.
The bitman.h is only a list of prototypes. The bitman.c is where all the functions are made and the main.c are where the we write the prototypes in the main to get the program working.
But I dont understand how to put up the rest. Hope someone can explane to me what to do.

****************************************************************

Write a small library that has the following functions:
 byte setBit(byte b, byte bitnum);
- Take the byte b and set bit number bitnum to 1. Return the result.
 byte clrBit(byte b, byte bitnum);
- Take the byte b and clear bit number bitnum. Return the result.
 byte getBit(byte b, byte bitnum);
- Take the byte b and return the value of bitnum .
 byte flpBit(byte b, byte bitnum);
- Take the byte b and flip bit number bitnum (0 -> 1, 1 -> 0). Return the result.
 byte getLoNibble(byte B )/>/ >/> ;
- Return the least significant nibble1
of the byte b.
 byte getHiNibble(byte B )/>/ >/> ;
- Return the most significant nibble of the byte b.
 byte bitrotL(byte B )/>/ >/> ;
- Rotate the byte b to the left one position. Return the result.
 byte bitrotR(byte B )/>/ >/> ;
- Rotate the byte b to the right one position. Return the result.
 char * bitStr(byte B )/>/>/> ;
- Take the byte b and convert it to an 8 character long text string.
Example: b=161 --> “10100001”. Hint: Use the hexit tool to check the output.
You may use functions from the stdlib, string, stdio libraries as needed.
Requirements:
 Define the type «byte» to be an 8 bit unsigned integer using a typedef declaration.
 Use assert() statements to verify that the input to the functions are within the correct
ranges. Also check all output values.
o Range for bitnum: [0..7]
 The small library shall be contained within a separate compilation unit. That is, a separate C
file with an associated header file.
 Write a test program that checks boundaries for the input variables for all functions. Check
also all output

***********************************************************'

This is my h.file this fare

#ifndef BITMAN2_H_INCLUDED
#define BITMAN2_H_INCLUDED

typedef unsigned int byte[8];

#endif // BITMAN2_H_INCLUDED


Is that a good start, or am I doing it wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Really need help. bits and bytes

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2254
  • View blog
  • Posts: 4,343
  • Joined: 30-May 10

Re: Really need help. bits and bytes

Posted 05 October 2014 - 05:05 AM

Yes, you're wrong so far.
But your include guards are fine :)

>  Define the type «byte» to be an 8 bit unsigned integer using a typedef declaration.
There is no mention of 'array' in this specification.

#ifndef BITMAN2_H_INCLUDED
#define BITMAN2_H_INCLUDED

typedef unsigned char byte;

// byte setBit(byte b, byte bitnum);
//- Take the byte b and set bit number bitnum to 1. Return the result.
// byte clrBit(byte b, byte bitnum);
//- Take the byte b and clear bit number bitnum. Return the result.
// byte getBit(byte b, byte bitnum);
//- Take the byte b and return the value of bitnum .
// byte flpBit(byte b, byte bitnum);
//- Take the byte b and flip bit number bitnum (0 -> 1, 1 -> 0). Return the result.
// byte getLoNibble(byte B )/>/ >/> ;
//- Return the least significant nibble1
//of the byte b.
// byte getHiNibble(byte B )/>/ >/> ;
//- Return the most significant nibble of the byte b.
// byte bitrotL(byte B )/>/ >/> ;
//- Rotate the byte b to the left one position. Return the result.
// byte bitrotR(byte B )/>/ >/> ;
//- Rotate the byte b to the right one position. Return the result.
// char * bitStr(byte B )/>/>/> ;
//- Take the byte b and convert it to an 8 character long text string. 

#endif // BITMAN2_H_INCLUDED



You put the same in bitman.c as well.

Then you
- uncomment // byte setBit(byte b, byte bitnum) in the header file
- write the implementation for it in bitman.c
- write a test case (or two) in main.c

Repeat until you've implemented the API.
Was This Post Helpful? 1
  • +
  • -

#3 kristian1986   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 14

Re: Really need help. bits and bytes

Posted 05 October 2014 - 09:13 AM

Thank you so much :D

I know this site is not for making people do my homework. But I am new to C-code. Only been doing it for 6 weeks.
But when you have the first part of the exercise;

/ byte setBit(byte b, byte bitnum);
//- Take the byte b and set bit number bitnum to 1. Return the result.

Can someone show me what should be writen in the headerfile, and the bitman.c file just to understand the first one?
Then I can try much more on the rest my self. Just need help to understand how to set it up correct, so I have someting to look at when I do the rest.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5611
  • View blog
  • Posts: 17,304
  • Joined: 25-December 09

Re: Really need help. bits and bytes

Posted 05 October 2014 - 09:55 AM

Also posted here.

Edit: And here.


Jim

This post has been edited by jimblumberg: 05 October 2014 - 10:13 AM

Was This Post Helpful? 1
  • +
  • -

#5 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Really need help. bits and bytes

Posted 05 October 2014 - 01:39 PM

You answered your own question. Though main.c should just #include 'bitman.c';

View Postkristian1986, on 05 October 2014 - 10:13 AM, said:

Can someone show me what should be writen in the headerfile, and the bitman.c file just to understand the first one?
Then I can try much more on the rest my self. Just need help to understand how to set it up correct, so I have someting to look at when I do the rest.


View Postkristian1986, on 05 October 2014 - 05:37 AM, said:

As far as I understand I have 3 files. The main.c, the bitman.c and the bitman.h.
The bitman.h is only a list of prototypes. The bitman.c is where all the functions are made and the main.c are where the we write the prototypes in the main to get the program working.


Before you start making the functions. In int main() practice using that byte type define.
And see what these operators do >> << ^ & | they are all binary operators. ^= |= &= <<= >>= also exist.
Just experiment with it. And read what they should do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1