Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

BASH scripting and bit shifting

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
NigelPH



Joined: Sun Oct 22, 2006 12:58 pm
Posts: 2
Location: London, UK

PostPosted: Sun Oct 22, 2006 3:27 pm    Post subject: BASH scripting and bit shifting Reply with quote

I am an occassioal dabbler in this sort of thing when the need arises and being a lapsed SAS programmer I can usually figure things out by tracking stuff down on the web, but this has got me stumped (I used to work with SMF on MVS. so decoding and bit testing using SAS was a doddle).

I have a 256 character hex string, which is the 128 byte EDID string from the attached monitor, the VESA spec states that the Manufacturer 3 character code is contained in 2 bytes, each character is represented by 5 bits of the 16 in those 2 bytes, using the 5 bits as decimal 1=A 2=B 3=C etc.. So using a pencil and scientific calculator I have figured out when I need to do but cannot find any function in BASH to perform them.
My calculations give me...
as Hex = 15c3
as binary = 0001010111000011
so dropping the first bit, then 00101=5=E, 01110=14=N, 00011=3=C, and I get ENC which apparently the code for EIZO.

It is probably not possible in BASH script so may have to look at Perl or similar to do it.

Any comments would be welcome.

Cheers,
Nigel


Last edited by NigelPH on Sun Oct 22, 2006 5:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8450
Location: Warrington, UK

PostPosted: Sun Oct 22, 2006 4:20 pm    Post subject: RE: BASH scripting and bit shifting Reply with quote

You may be able to do this with bc. I've only used it for floating point calculations in Bash scripts, but fro the man page it appears to do far more.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
NigelPH



Joined: Sun Oct 22, 2006 12:58 pm
Posts: 2
Location: London, UK

PostPosted: Sun Oct 22, 2006 9:19 pm    Post subject: Reply with quote

Thanks for that, I had a quick look at BC and then I had an idea, I had notriced the bitwise shifting >> and << that all the pages I visited mentioned they were C like but seldom used.

So by shifting and translating to hex and truncating back to one byte, then shifting again etc..., I managed to figure it out, it went like this (probably a bit long winded)

# 1st 5 bits - just shift the 1st byte shift left 2 bits ( I ignored bit0 )
MFR1=`expr substr $EDIDSTR 17 2`
MFR1=$(printf "%d\n " 0x$MFR1)
MFR1=$(($MFR1 >> 2))
#middle 5 bits - take both bytes, shift right 5 bits, keep the last byte,
# shift left 3 bits, keep the last byte, shift right 3 bits
MFR2=`expr substr $EDIDSTR 17 4`
MFR2=$(printf "%d\n " 0x$MFR2)
MFR2=$(($MFR2 >> 5))
MFR2=$(printf "%X\n " $MFR2)
MFR2L=`expr length $MFR2`
if [ $MFR2L -gt 2 ]
then
MFR2L=$(($MFR2L - 1))
MFR2=`expr substr $MFR2 $MFR2L 2`
fi
MFR2=$(printf "%d\n " 0x$MFR2)
MFR2=$(($MFR2 << 3))
MFR2=$(printf "%X\n " $MFR2)
MFR2L=`expr length $MFR2`
if [ $MFR2L -gt 2 ]
then
MFR2L=$(($MFR2L - 1))
MFR2=`expr substr $MFR2 $MFR2L 2`
fi
MFR2=$(printf "%d\n " 0x$MFR2)
MFR2=$(($MFR2 >> 3))
# last 5 bits - take second byte, shift left 3 bits, keep the last, shift right 3 bits
MFR3=`expr substr $EDIDSTR 19 2`
MFR3=$(printf "%d\n " 0x$MFR3)
MFR3=$(($MFR3 << 3))
MFR3=$(printf "%X\n " $MFR3)
MFR3L=`expr length $MFR3`
if [ $MFR3L -gt 2 ]
then
MFR3L=$(($MFR3L - 1))
MFR3=`expr substr $MFR3 $MFR3L 2`
fi
MFR3=$(printf "%d\n " 0x$MFR3)
MFR3=$(($MFR3 >> 3))
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast