LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 11:43 AM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370

Rep: Reputation: 8
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, 12:01 PM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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 12:10 PM.
 
Old 04-24-2017, 12:06 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,962
Blog Entries: 4

Rep: Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024Reputation: 4024
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, 12:26 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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, 12:37 PM   #5
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370

Original Poster
Rep: Reputation: 8
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, 12:59 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,225

Rep: Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687Reputation: 7687
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, 01:58 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 02:40 PM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,225

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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, 03:20 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,351

Rep: Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382Reputation: 5382
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 04:29 PM.
 
Old 04-24-2017, 10: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-25-2017, 12:21 AM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,306
Blog Entries: 24

Rep: Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259
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-25-2017 at 12:22 AM. Reason: argv[1] -> $1
 
1 members found this post helpful.
Old 04-25-2017, 01:26 AM   #12
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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, 05:53 AM   #13
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370

Original Poster
Rep: Reputation: 8
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 02:35 AM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 11:38 AM
How to decode hex-octets to 8-bit octets, than to 7-bit default alphabet ? darius1 Linux - Software 2 04-18-2009 07:07 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 01:05 PM
printing 64 bit hex number lilzz Linux - Newbie 8 08-20-2006 09:16 PM

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

All times are GMT -5. The time now is 06:36 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