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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
04-24-2017, 11:43 AM
|
#1
|
Member
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370
Rep:
|
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
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?
|
|
|
04-24-2017, 12:01 PM
|
#2
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
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.
|
|
|
04-24-2017, 12:06 PM
|
#3
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,962
|
You simply need to write your script in a programming language that knows how to do this: - Convert the hexadecimal-encoded string to its binary result.
- Shift the value '1' to the left n-1 bits.
- Logic-AND the two results together.
- 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."
|
|
|
04-24-2017, 12:26 PM
|
#4
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
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.
|
|
|
04-24-2017, 12:37 PM
|
#5
|
Member
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370
Original Poster
Rep:
|
Quote:
Originally Posted by hydrurga
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
|
|
|
04-24-2017, 12:59 PM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,225
|
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
|
|
|
04-24-2017, 01:58 PM
|
#7
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
04-24-2017, 02:40 PM
|
#8
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,225
|
In Perl:
Code:
BIT=0 # or 1, 2, 3
perl -e '$bit = hex(`./arco`) >> $ARGV[0]; print 1 & $bit,"\n"' $BIT
|
|
|
04-24-2017, 03:20 PM
|
#9
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,351
|
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.
|
|
|
04-25-2017, 12:21 AM
|
#11
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,306
|
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.
|
04-25-2017, 01:26 AM
|
#12
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
Quote:
Originally Posted by Doug G
|
If you can handle the octal and binary running together.
|
|
|
04-26-2017, 05:53 AM
|
#13
|
Member
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 370
Original Poster
Rep:
|
Quote:
Originally Posted by astrogeek
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 !
|
|
|
All times are GMT -5. The time now is 06:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|