LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-24-2017, 10:43 AM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 196

Rep: Reputation: 4
Get value of particular bit in hex digit


I have unit connected to USB. It is 4 digital input device, so I can read state of inputs by command

Code:
./arco
It returns one hexadecimal digit representing state of inputs.
My question is. How can I in most simplest way, know value of particular bit in this digit? The answer should be 0 or 1.
Any idea?
 
Old 04-24-2017, 11:01 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
One hex digit is 4 bits. Therefore, it cannot be represented as 0 or 1. The entire value could be 0 or 1, but it could be greater than that. If you're looking at one bit of a hex digit, usually people just memorize the bit positions.
Code:
0000=0
0001=1
0010=2
0011=3
0100=4
0101=5
0110=6
0111=7
1000=8
1001=9
1010=10
1011=a
1100=b
1101=c
1110=d
1111=f

Last edited by AwesomeMachine; 04-24-2017 at 11:10 AM.
 
Old 04-24-2017, 11:06 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
You simply need to write your script in a programming language that knows how to do this:
  1. Convert the hexadecimal-encoded string to its binary result.
  2. Shift the value '1' to the left n-1 bits.
  3. Logic-AND the two results together.
  4. Test whether the result is or is not zero.

Although I am quite sure ... ... that some future respondent will show us how "bash scripting" can do this," I would instead recommend that you write your script in "a real programming language" (I can think of more than half-a-dozen, right off the bat) that really knows how to do this.

Then, by prefixing your script with #!language_processor_name, aka "shebang," you can use that language to "write your script."
 
Old 04-24-2017, 11:26 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861Reputation: 2861
If $test contains the hex digit (in numeric form),

Code:
echo $((($test & 2#0010) != 0))
will give you 0 or 1 indicating whether the second least significant bit is set.
 
Old 04-24-2017, 11:37 AM   #5
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 196

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by hydrurga View Post
If $test contains the hex digit (in numeric form),

Code:
echo $((($test & 2#0010) != 0))
will give you 0 or 1 indicating whether the second least significant bit is set.
I use sh not bash and probably because of that this line returns:

Code:
./test: line 7: arithmetic syntax error
 
Old 04-24-2017, 11:59 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,083

Rep: Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141
based on post #2 I would try to make a switch/case:
Code:
case <your input> in
 0) a=0000 ;;
 1) a=0001 ;;
....
esac
and you will need to pick the particular bit of string a
 
Old 04-24-2017, 12:58 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
Code:
echo "obase=2; ibase=16; $var" | bc -l
will convert $var from hex to binary and print it out, then you just need to grab the appropriate digit.
 
Old 04-24-2017, 01:40 PM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,209

Rep: Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871
In Perl:

Code:
BIT=0  # or 1, 2, 3
perl -e '$bit = hex(`./arco`) >> $ARGV[0]; print 1 & $bit,"\n"'  $BIT
 
Old 04-24-2017, 02:20 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,149

Rep: Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972
If you're using BASH and DIGIT has the hex digit as a string (e.g. "A"):

First bit:

Code:
if "$(( 0x${DIGIT} & 1 ))" == "1"
Second digit:
Code:
if "$(( 0x${DIGIT} & 2 ))" == "2"
Third bit:

Code:
if "$( 0x${DIGIT} & 4 ))" == "4"
Fourth bit:
Code:
if "$(( 0x${DIGIT} & 8 ))" == "8"

Last edited by dugan; 04-24-2017 at 03:29 PM.
 
Old 04-24-2017, 09:55 PM   #10
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
Simplest for me would be to refer to this table: http://ascii.cl/conversion.htm
 
Old 04-24-2017, 11:21 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,221
Blog Entries: 11

Rep: Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172
This will give the bit state as 0 or 1 for each bit, where HEXD is a single hex digit:

Code:
#!/bin/sh
HEXD=A;
echo "Bit 3 = $(( (0x${HEXD} & 8) > 0))"
echo "Bit 2 = $(( (0x${HEXD} & 4) > 0))"
echo "Bit 1 = $(( (0x${HEXD} & 2) > 0))"
echo "Bit 0 = $(( (0x${HEXD} & 1) > 0))"
You could put it into a loop, but if you only need the lower four bits this is still simple and explicit.

As your arco function returns one hex digit, simply substitute $1 for HEXD, save to script, say hexbits, then suppose arco returns 'A':

Code:
./arco | hexbits
Bit 3 = 1
Bit 2 = 0
Bit 1 = 1
Bit 0 = 0

Last edited by astrogeek; 04-24-2017 at 11:22 PM. Reason: argv[1] -> $1
 
1 members found this post helpful.
Old 04-25-2017, 12:26 AM   #12
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
Quote:
Originally Posted by Doug G View Post
Simplest for me would be to refer to this table: http://ascii.cl/conversion.htm
If you can handle the octal and binary running together.
 
Old 04-26-2017, 04:53 AM   #13
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 196

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by astrogeek View Post
This will give the bit state as 0 or 1 for each bit, where HEXD is a single hex digit:

Code:
#!/bin/sh
HEXD=A;
echo "Bit 3 = $(( (0x${HEXD} & 8) > 0))"
echo "Bit 2 = $(( (0x${HEXD} & 4) > 0))"
echo "Bit 1 = $(( (0x${HEXD} & 2) > 0))"
echo "Bit 0 = $(( (0x${HEXD} & 1) > 0))"
You could put it into a loop, but if you only need the lower four bits this is still simple and explicit.

As your arco function returns one hex digit, simply substitute $1 for HEXD, save to script, say hexbits, then suppose arco returns 'A':

Code:
./arco | hexbits
Bit 3 = 1
Bit 2 = 0
Bit 1 = 1
Bit 0 = 0
THX. That resolved my problem !
 
  


Reply

Tags
bits, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Passable nouveau kernel driver bug (MMIO read of [hex l] FAULT at [hex l]) marbangens Linux - General 1 05-24-2013 01:35 AM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
How to decode hex-octets to 8-bit octets, than to 7-bit default alphabet ? darius1 Linux - Software 2 04-18-2009 06:07 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
printing 64 bit hex number lilzz Linux - Newbie 8 08-20-2006 08:16 PM

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

All times are GMT -5. The time now is 01:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration