LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2017, 12:10 PM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 367

Rep: Reputation: 8
Bit operation in SHELL script


I have unit connected to my Linux server over USB (named ARCO). When I "ask" one about state using command:

Code:
./arco
it returns 2 hex digits like "a7", bf" etc. First digit represents state of inputs and second state of outputs.
I can change output states by command like:

Code:
./arco 5
and it set outputs no 1 and 3 to "1", and outputs 2 and 4 to "0".

My question is. How can I set or clear particular output/bin without changing another in shell script, using command

Code:
./arco x
where x=0 to 15
 
Old 04-23-2017, 12:55 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
You could use printf to convert from binary to hexadecimal:

Code:
printf '%x\n' "$((2#101010101))"
But that overwrites everything.

If you want to keep the state of the bits, then it is just a matter of tracking the individual bits with variables:

Code:
d0=1;
d1=0;
d2=0;
d3=1;
printf '%x\n' "$((2#$d3$d2$d1$d0))";
Extend as needed.

Last edited by Turbocapitalist; 04-23-2017 at 12:56 PM.
 
1 members found this post helpful.
Old 04-23-2017, 01:41 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
If the hex representation of the current output state is in variable OUT and you want to set bit 3:
Code:
./arco $((0x$OUT | 8))
To clear bit 3:
Code:
./arco $((0x$OUT & ~8))
Those are both passing the value to ./arco in decimal. If it needs to be in hex, you'll need to use the printf command to convert.
 
1 members found this post helpful.
Old 04-23-2017, 02:25 PM   #4
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 367

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by rknichols View Post
If the hex representation of the current output state is in variable OUT and you want to set bit 3:
Code:
./arco $((0x$OUT | 8))
To clear bit 3:
Code:
./arco $((0x$OUT & ~8))
Those are both passing the value to ./arco in decimal. If it needs to be in hex, you'll need to use the printf command to convert.
THX. It resolved my problem
 
  


Reply

Tags
bits, shell script



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
shell script for matrix operation dynamics Programming 6 09-19-2010 08:34 PM
a new shell for doing mathematical operation tubu_cuteguy Linux - Newbie 5 07-28-2010 07:16 PM
[SOLVED] A bit confused with && and || operators in shell script luvshines Programming 6 11-24-2009 12:37 PM
Which program should I use to execute a shell script In Ubuntu 8.04 64-bit? joel.breger Linux - Newbie 4 05-26-2008 10:29 AM
Help needed to understand a bit of shell script yogeshm02 Programming 8 02-03-2008 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:25 PM.

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