LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2015, 03:56 AM   #1
Flowsen
LQ Newbie
 
Registered: Sep 2011
Posts: 29

Rep: Reputation: Disabled
Question Print n position of a binary pattern in C


Dear Community,

hopefully someone can help me with this "small" problem.
I got a byte, that is converted to binary and then printed to screen.

Code:
#include <stdio.h>
#include <stdlib.h>

#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte)  \
  (byte & 0x80 ? 1 : 0), \
  (byte & 0x40 ? 1 : 0), \
  (byte & 0x20 ? 1 : 0), \
  (byte & 0x10 ? 1 : 0), \
  (byte & 0x08 ? 1 : 0), \
  (byte & 0x04 ? 1 : 0), \
  (byte & 0x02 ? 1 : 0), \
  (byte & 0x01 ? 1 : 0)
Code:
printf(BYTETOBINARYPATTERN, BYTETOBINARY(buf[0]));
This outputs something like "00010100"
How can I print only a specific postion of this binary? For example the second, third position and so on...

Furthermore, how can I print a specific position and swap the bit. For example output 1 instead of 0 or 0 instead of 1.

Any help would be greatly appreciated!
 
Old 04-23-2015, 05:57 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 04-23-2015 at 06:07 AM.
 
1 members found this post helpful.
Old 04-23-2015, 06:22 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
OP should read a good 'C' tutorial or a basic 'C' book.

'The C programming language' by Kernighan and Ritchie is good one and covers ANSI C90.
 
Old 04-23-2015, 06:51 AM   #4
Flowsen
LQ Newbie
 
Registered: Sep 2011
Posts: 29

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-23-2015, 09:14 AM   #5
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
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.
 
Old 04-23-2015, 09:22 AM   #6
Flowsen
LQ Newbie
 
Registered: Sep 2011
Posts: 29

Original Poster
Rep: Reputation: Disabled
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)...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting the pattern of the value to print snehasishmohanta Linux - Newbie 1 01-08-2015 07:36 PM
GAWK Find Pattern Print Lines Before and After metallica1973 Programming 3 11-07-2013 09:12 PM
[SOLVED] print 2nd value of matching pattern progchi Linux - General 2 03-22-2013 06:55 AM
Print a line on n'th position within a large file...will PIPE optimize this? f1dg3t Linux - Newbie 6 03-19-2010 02:41 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration