LinuxQuestions.org Get value of particular bit in hex digit
 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!

 04-24-2017, 11:43 AM #1 mackowiakp Member   Registered: Jun 2014 Location: Poland/Gdynia Distribution: Mageia 9, SH4, Debian Posts: 367 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 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?
 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: 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 sundialsvcs LQ Guru   Registered: Feb 2004 Location: SE Tennessee, USA Distribution: Gentoo, LFS Posts: 10,552 Blog Entries: 4 Rep: 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 hydrurga LQ Guru   Registered: Nov 2008 Location: Pictland Distribution: Linux Mint 21 MATE Posts: 8,048 Blog Entries: 5 Rep: 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
mackowiakp
Member

Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 367

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 pan64 LQ Addict   Registered: Mar 2012 Location: Hungary Distribution: debian/ubuntu/suse ... Posts: 21,478 Rep: based on post #2 I would try to make a switch/case: Code: ```case 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 suicidaleggroll LQ Guru Contributing Member   Registered: Nov 2010 Location: Colorado Distribution: OpenSUSE, CentOS Posts: 5,573 Rep: 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 smallpond Senior Member   Registered: Feb 2011 Location: Massachusetts, USA Distribution: Fedora Posts: 4,112 Rep: 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 dugan LQ Guru   Registered: Nov 2003 Location: Canada Distribution: distro hopper Posts: 11,184 Rep: 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-24-2017, 10:55 PM #10 Doug G Member   Registered: Jul 2013 Posts: 749 Rep: Simplest for me would be to refer to this table: http://ascii.cl/conversion.htm
 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,249 Blog Entries: 24 Rep: 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
AwesomeMachine
LQ Guru

Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep:
Quote:
 Originally Posted by Doug G 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.

04-26-2017, 05:53 AM   #13
mackowiakp
Member

Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 367

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 !

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post marbangens Linux - General 1 05-24-2013 02:35 AM dmason165 Programming 13 08-07-2009 11:38 AM darius1 Linux - Software 2 04-18-2009 07:07 PM mlewis Programming 35 04-10-2008 01:05 PM lilzz Linux - Newbie 8 08-20-2006 09:16 PM

LinuxQuestions.org

All times are GMT -5. The time now is 05:28 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -