[SOLVED] Print n position of a binary pattern in C
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sounds like homework.
It also is so trivial a beginner at C programming should have no trouble. So I suspect you have been relying too much on finding, asking for, copying etc. answers rather than learning what you were expected to learn.
The important operator in C that you should use for this task is << or >>
Look those up and see if you can understand them and see if you can figure out how one of them can do most of the work that you are asking for.
The & operator is also important for this task. But if you have any understanding of the code you posted, then you know how to use the & operator.
Before writing code to use numbered positions in a byte (as you said "For example the second, third position") make sure you number positions the way your instructor wants them numbered.
An experienced software engineer would normally number the rightmost bit in a byte as bit 0, and the one directly on its left bit 1, and increasing right to left. But a program interacting with a person who is not supposed to be a software engineer might be expected to start from 1 on the left rather than 0 on the right (so positions are 1 up to 8 from left to right, rather than 7 down to 0 from left to right). I don't want to guess which way your instructor wants them numbered. You should either read the assignment carefully to get that, or ask for clarification.
To be honest I am no C programmer not even a beginner.
I agree, reading a book about C and learning it step by step would solve my problem.
As I am currently only stuck on the two trivial problems I thought someone could help me out.
I am sorry this question was too simple but I would really appreciate your help.
Start by analyzing what the macros do in conjunction with the printf statement. The printf specification is for 8 unsigned ints and there are 8 arguments in the second macro. Each one uses a hex representation of a number to turn on the bit in the position in the byte that is the same as the position it holds in the printf argument. It then test for truth, using the ternary operator and the logical AND operator, return a 1 in that position if true or 0 if false.
The logical AND makes a bit by bit comparison of the bit pattern such that 1 & 1 = 0, 0 & 0 = 0, 1 & 0 = 0, 0 & 1 = 0. Each of the masks (the patterns in the printf arguments) is selected because only one bit is set to 1, i.e. 0x80 = 10000000. If we look at the possible outcomes of comparing any bit we can see that only bits in the leftmost position can be anything but 0 when masked i.e. 10000000 & 00000001 == 00000000. In C a 0 value is false and anything else is true so that example would give a 0 in the printf statement test for the leftmost position. Running another number through, i.e. 10000000 & 10000001 == 10000000 gives us a true value and the ternary test will print a 1.
If you look at each of the numbers represented in the ternary tests you can see they represent a single bit set at the position they occupy in the arguments. Using this information you should be able to reason out how to print just the one positional bit.
The ! operator is the not operator. If its argument has a true value it returns false (0). If its argument is false it returns true (1).
The other logical operators are | (or) and ^ (xor). 1 | 1 = 1, 1 | 0 = 1, 0 | 0 = 0 and 1 ^ 1 = 0, 1 ^ 0 = 0, 0 ^ 0 = 0.
Thank you.
I managed to solve my problem with your explanation by changing the macro to the specific hex value and returning only a single bit (true or false)...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.